GOOD - Recursion - in - The - AIME
GOOD - Recursion - in - The - AIME
Author: For:
Jeffrey Chen AoPS
Date:
May 6, 2020
0
1
x
y 2
3
1 0
2
1
3 z
0 2
1 3
0
z2 1
2
30
1
2
y
x 3
Contents
0 Acknowledgements 4
1 Introduction to Recurrence 5
4 Catalan Numbers 7
5 Exercises 9
3
Jeffrey Chen (May 6, 2020) Recursion in the AIME2
§0 Acknowledgements
This was made for the Art of Problem Solving Community out there! I would like to thank Evan Chen for his
evan.sty code. In addition, all problems in the handout were either copied from the Art of Problem Solving
Wiki or made by myself. AoPS User freeman66 helped format the document.
4
Jeffrey Chen (May 6, 2020) Recursion in the AIME3
§1 Introduction to Recurrence
”To understand recursion, we must first understand recursion.” This is a common joke about recursion that
brings confusion for those that don’t understand it but laughter for those who do. The heart of recursion is to
assume that you know previous elements of a sequence and then find a way to construct the next element in that
sequence. It is a common technique to solve weird looking counting problems that traditional methods including
the Principal of Inclusion-Exclusion, casework, and complementary counting can’t solve. Instead of directly
counting the number of objects, we assume that we know the number of objects in a smaller case, then try to
find a relationship and build up to larger cases. This may sound confusing at first, but with enough practice,
you will certainly be able to understand how and why it works. The best way to learn any sort of skill is with
practice, and the same is true for recursion.
Suppose we have a sequence, a1 , a2 , a3 , · · · , and assume we also have a relationship between terms of the
sequence, such as
an = 3 · an−1
This is what is known as a recurrence relationship: expressing a term of a sequence as some particular function
of the preceding terms. Taking our original recurrence relationship - an = 3 · an−1 , we can translate this to every
term in the sequence is 3 times the last term in the sequence. If we let the first term, a1 , be equal to 1, then we
can see that
a2 = 3, a3 = 9, a4 = 27
and so on. The important thing about the recurrence relationship is that the relationship is true for all n. For
example, a40 = 3 · a39 just as a100 = 3 · a99 . Because of this, we can sometimes make substitutions. For example,
since an = 3 · an−1 and an−1 = 3 · an−2 , substituting the second equation into the first gives us an = 9 · an−2 .
Sticking with our recurrence relation of an = 3 · an−1 , and given that a1 = 1, we can solve for the rest of the
terms of the recurrence relation. We can make a table to calculate each value of ai :
Sequence of an
i 1 2 3 4 5 6 7
ai 1 3 9 27 81 243 729
Looking at this, it seems like all the ai are powers of 3! More specifically, it seems like ai = 3i−1 . Proving this
by methods such as induction is left as an exercise to the reader.
Given the first few terms of a recursion and it’s recursive relationship, it is possible to get a formula for ai .
However, in short answer competitions such as the AMC and AIME, finding the formula usually isn’t necessary.
Just by using the recursive relationships, it is possible to compute ai by making a table and expressing ai as
a function of the preceding terms. If you want to learn how to get an explicit formula for a general linear
recurrence, this Wikipedia article explains it quite well.
Example 2.1
Fred wants to climb a 10 step staircase. He can climb either 1 or 2 steps at a time. In how many ways can
he climb the staircase?
Solution. We might try to count this directly. Perhaps we could list out all the possible combinations of how
Fred could walk, but using traditional methods such as complementary counting, Principle of Inclusion-Exclusion,
and casework won’t work very well. Instead, we will use recursion.
5
Jeffrey Chen (May 6, 2020) Recursion in the AIME4
Instead of thinking of this problem as ”Fred wants to climb 10 stairs”, we will think of it more generally as
”Fred wants to climb n stairs.” Then, we can define
an = The number of ways for Fred to walk n steps of stairs.
With recursion in mind, we want to try to find a way to relate an to the preceding terms. Think of the last step
that Fred takes. This step can either be one stair or two stairs. If the last step is two stairs, then Fred would
have climbed n − 2 stairs and then climbed two more stairs. The number of ways for Fred to climb n − 2 stairs
is an−2 due to our definition of ai , so the number of ways for Fred to climb n − 2 stairs and then climb 2 steps is
an−2 . If the last step is one stair, then Fred would have climbed n − 1 stairs and then climbed one stair. The
number of ways for this to happen is an−1 . Since these are the only two possibilities - either Fred’s last step is
one or two stairs, we have the following equation:
an = an−1 + an−2
Now we can solve the problem. It is easy to see that a1 = 1 as Fred can climb 1 stair in only 1 way, and a2 = 2
because Fred can either climb 2 stairs in 2 one-stair steps or 1 two-stair step. We can make a table to solve for
a10 :
Sequence of an
i 1 2 3 4 5 6 7 8 9 10
ai 1 2 3 5 8 13 21 34 55 89
Some of you may recognize this sequence as the Fibonacci sequence, which appears quite often in counting
problems and especially ones that use recursion. Knowing the first few elements of the Fibonacci sequence can
sometimes be handy for certain speed based competitions. As a side note, the Fibonacci numbers have a lot of
interesting properties.
Solution. Counting directly seems to be somewhat difficult since there would be a lot of casework involved, so
we think of recursion. We will define
an = The number of spacy subsets of the set {1, 2, . . . , n}
Next, we will try to relate an to the terms preceding it. For each subset, we can put it into one of two groups:
whether the subset contains n or does not contain n. If the subset contains n, then because it is spacy, that
subset cannot contain n − 1 or n − 2. Thus, this spacy subset is simply a spacy subset of {1, 2, . . . , n − 3} with
an appended n. Thus, there must be an−3 spacy subsets that contain n. If a spacy subset does not contain n,
then it is also a spacy subset of {1, 2, . . . , n − 1}, so there are an−1 spacy subsets that do not contain n. Since a
spacy subset either contains n or does not contain n, we have exhausted all the possibilities. This results in the
following recursive relationship:
an = an−1 + an−3
We can calculate by hand the first few values of ai . We have a1 = 2 since the two spacy subsets are {} and {1}.
Similarly, a2 = 3 since the spacy subsets are {}, {1}, {2}, and a3 = 4 since the spacy subsets are {}, {1}, {2}, {3}.
Now, with these values, we can make a table and solve for a12 :
6
Jeffrey Chen (May 6, 2020) Recursion in the AIME5
Sequence of an
i 1 2 3 4 5 6 7 8 9 10 11 12
ai 2 3 4 6 9 13 19 28 41 60 88 129
• The cube immediately on top of a cube with edge-length k must have edge-length at most k + 2.
Then we look at the cube with edge-length n. This cube can either be on top of the cube with edge length n − 1,
on top of the cube with edge length n − 2, or it could be at the bottom of the tower. Thus, for each configuration
with n − 1 cubes, there are 3 ways to create a configuration with n cubes, and we have the following equation:
an = 3 · an−1
Since a2 = 2 as we can either have the cube with edge-length 1 on the bottom or the cube with edge-length 2 on
the bottom, we have a8 = 36 · 2 = 1458 .
Observe that we can’t say that a2 = 3 · a1 . This is because our recurrence relationship breaks down when
n = 2. We assumed that the cube with edge-length n can either be placed on the bottom, above the cube with
edge-length n − 1, or above the cube with edge-length n − 2; however, when there is no cube with edge-length
n − 2, (as when we substitute n = 2 this becomes 0), the recurrence is no longer an = 3 · an−1 . Sometimes
for recurrences we can’t directly recur from the very beginning and have to find the first few terms before the
recurrence actually applies.
§4 Catalan Numbers
The Catalan numbers are best presented through the following problem:
Example 4.1
How many 2n step paths are there from (0, 0) to (n, n) such that for each (x, y) on that path, x ≥ y?
7
Jeffrey Chen (May 6, 2020) Recursion in the AIME6
Counting by hand we can see that there are 5 paths. But we could try to do this recursively. We will define
Cn = The number of paths from (0, 0) to (n, n) such that for each (x, y) on that path, x ≥ y.
Let the point (i, i) be the first point in a path where x = y, besides the point (0, 0). Then, we can see that the
number of ways to get from (i, i) to (n, n) is Cn−i . However, the number of ways to get from (0, 0 to (i, i) is
Ci−1 . Note that if (i, i) is the first point in the path where x = y, then the path had to go from (0, 0) to (1, 0) to
(i, i − 1) to (i, i). Then, the portion of the path from (1, 0) to (i, i − 1) must always be below the line x ≥ y + 1,
as illustrated below:
As we can see, the path never goes above the line x = y + 1 except at the very end. Then, the number of paths
that goes from (1, 0) to (i, i − 1) that doesn’t go above the line x = y + 1 has to be Ci as we can just translate
that portion of the path 1 unit to the left, and we get a graph from (0, 0) to (i − 1, i − 1) that never goes above
the line x = y. Therefore, the number of paths that intersect the line y = x first at point (i, i) will be Ci−1 Cn−i .
Summing this over all i, as the path can only touch the line x = y first at exactly one point, we get the following
recurrence:
Cn
= C0 Cn−1 + C1 Cn−2 + . . . + Cn−2 C1 + Cn−1 C0
n−1
X
= Ci Cn−i−1 .
i=0
We have found our recurrence relationship. This sequence is called the Catalan numbers, and it provides the
solution to many problems.
8
Jeffrey Chen (May 6, 2020) Recursion in the AIME7
Example 4.3
How many ways are there to arrange n open brackets ”[” and n closed brackets ”]” such that when read left
to right, the number of closed brackets is less than or equal to the number of open brackets?
Solution. We could try to form the recurrence again, but instead, we use a clever bijection to example 4.1. We
place an duck on point (0, 0). Then, whenever we have an open bracket ”[”, we will move the duck one unit
to the right. Whenever we have a closed bracket, we will move the duck one unit up. Since there are n open
brackets and n closed brackets, the duck will end up on point (n, n), and since the number of closed brackets is
always less than or equal to the number of open brackets, the duck will never go above the line x = y. Thus, the
solution to this problem is simply the Catalan numbers Cn .
§5 Exercises
Exercise 5.1. How many ways are there to tile an 10 by 2 board with 1 by 2 dominoes such that each
domino covers exactly two squares and no domino overlaps?
Exercise 5.2 (AMC 12 2019). How many binary sequences (sequences of 0’s and 1’s) of length 19 are there
that begin with a 0, end with a 0, contain no two consecutive 0’s, and contain no three consecutive 1’s?
Exercise 5.3 (AIME 2015). There are 210 = 1024 possible 10-letter strings in which each letter is either an
A or a B. Find the number of such strings that do not have more than 3 adjacent letters that are identical.
Exercise 5.4. Given a regular 2n-gon, how many ways are there to pair vertices and draw line segments
between those vertices such that no two line segments intersect?
Exercise 5.5 (Charmander3333’s Contrib test). Ms. Red wants to create a 10 question test for her students.
Each question is multiple choice with 4 answer choices, a, b, c, and d. How many ways are there for Ms. Red
to create the 10 question test if every 5 consecutive questions must contain at least one of each of the four
answer choices?
Exercise 5.6 (AIME 2001). A mail carrier delivers mail to the nineteen houses on the east side of Elm
Street. The carrier notices that no two adjacent houses ever get mail on the same day, but that there are
never more than two houses in a row that get no mail on the same day. How many different patterns of
mail delivery are possible?
9
Jeffrey Chen (May 6, 2020) Recursion in the AIME8
Exercise 5.7 (AIME 2016). The figure below shows a ring made of six small sections which you are to
paint on a wall. You have four paint colors available and will paint each of the six sections a solid color.
Find the number of ways you can choose to paint each of the six sections if no two adjacent section can be
painted with the same color?
Exercise 5.8. Given a stairstep shape of size n by n, where the n = 4 version is shown below,
how many ways are there to tile this n by n stairstep shape with n rectangles with integer lengths?
Exercise 5.9 (AIME 2018). Find the number of permutations of 1, 2, 3, 4, 5, 6 such that for each k with
1 ≤ k ≤ 5, at least one of the first k terms of the permutation is greater than k
Exercise 5.10 (AoPS Mock Contest). How many ways can an ant walk from point A to point B along the
arrows using exactly 8 steps?
A
Exercise 5.11 (Intermediate Counting and Probability). Count the number of sequences of integers
a1 , a2 , a3 , . . . , an with ai ≤ 1 for all i such that all partial sums a1 + a2 + . . . + ai are non-negative.
10