This section is not complete.
Overview
You may run across combinatorial problems that seem almost impossible to solve. They may involve a strenuous amount of casework or "bashing", but usually, many such problems can be solved via recursion. Recursion models a problem in terms of smaller versions of itself. In math competitions, recursion is especially useful in path counting, constrained arrangements, and step-by-step probability processes.
Recursion?
- Recursion is using smaller solutions of a problem to solve bigger ones. For instance, if we let be a solution for a problem considering parameters, we may be able to get in terms of . may be something like the number of certain arrangements of a sequence of orders considering the first of them, or something completely different. It depends on the problem!
- Recursion needs a state, transition rule, and a base case:
- A state is , the value we're trying to compute as a function of some parameter .
- A transition rule is an equation expressing one state in terms of smaller ones. For instance, is a transition rule. Using this, we can get solely by using and .
- A base case is a small case like or that we can solve manually without using recursion.
Demonstration
Consider the following combinatorial problem:
How many arrangements are there of distinct values?
Clearly, this is just . However, to demonstrate recursion, we will transform this problem. Let denote the number of arrangements of numbers where . If we know , we can also find . Since considers one more value than , there are places to insert that new value into each of the arrangements. Hence:
For our base case, we consider . Obviously, there is only one way to arrange value, so . Writing out a couple more values using our transition, we see:
- ...
We can see recursion also gives , as desired.
Use Cases
Recursion is often simpler but a more tedious method than conventional combinatorial techniques. One can find a couple base cases, a transition statement, and then use recursion to iteratively find their answer as they work upwards/downwards.
Worked Example
How many ways can you climb a staircase of 5 steps if each move is 1 or 2 steps?
We will solve this using recursion. As always, we begin with a base case. For , there is only one step, so . For , we can climb the staircase in two ways:
- Take two steps of length .
- Take one step of length .
Hence, . Now we come up with a transition statement. Observe that for where , the last move in any valid climb is either:
- A step of length , preceded by some arrangement climbing steps. There are such arrangements.
- A step of length , preceded by some arrangement climbing steps. There are such arrangements.
These two cases are disjoint (they differ in the final move) and cover every possibility, so:
Applying this up to :
Dynamic Programming
Look again at the staircase example. To get we used and , but computing also needed . If we restarted the breakdown every time we met a smaller state, the same values would get recomputed over and over, and the work would pile up fast. The fix is to write each value down once and reuse it.
This is the idea behind dynamic programming, a common technique used in competitive programming. Rather than starting at and breaking it down, we start from the base cases and build upward (bottom-up), recording each result:
Every entry depends only on entries already written, so a single left-to-right pass finishes the count. On a contest this is usually all that recursion means in practice. You find the base cases, find the transition, then fill in a line of numbers until you reach the one you want.
Recursion and dynamic programming are two views of the same idea. The transition describes how states relate to each other, and dynamic programming is the bookkeeping that turns that relationship into numbers without wasted effort.
One more tool comes for free. If a state is forbidden, say a step you are not allowed to land on, set its value to and keep filling as usual. Arrangements that would have passed through it contribute nothing, and every other count stays correct.
Choosing a Good State
Writing the transition and base cases is usually the easy part. The real skill is choosing the state, meaning deciding what information should depend on. A useful state has two properties:
- It carries everything you need to keep counting. Once you know the state, the number of ways to finish should not depend on how you got there.
- It stays small. Fewer parameters means fewer values to track and less room for error.
In every example so far a single number was enough: the step you are on, the height of the staircase, the count of values arranged. Some problems need more.
A Two-Dimensional Example
Starting at the bottom-left corner of a grid of unit squares, how many paths reach the top-right corner if every move goes one unit right or one unit up?
One number cannot say where you are on a grid, since a position needs two coordinates. So let be the number of paths from the start to the point units right and units up. To arrive at , the last move came either from the left, from , or from below, from . These cases are disjoint and cover everything, so:
The base case is the starting corner, . Every point on the bottom edge or the left edge has exactly one path, reached by walking straight along that edge, so .
Filling the grid from the bottom-left upward, with rows for down to and columns for to , gives:
The top-right entry is . As a check, a path uses rights and ups in some order, and choosing which of the moves are ups gives , matching the table. Grid path questions like this one appear regularly on the AMC 8 and AMC 10!
This is the same transition as the staircase, now spread across two directions. Whenever a problem tracks a position on a grid, an amount of two separate resources, or any pair of quantities that change independently, a two-parameter state is usually the right place to start. Don't be afraid by having multi-dimensional recursion states!
Recognizing These Problems
On the AMC, a few signals suggest recursion or dynamic programming will work:
- The problem builds up step by step, like climbing stairs or extending a sequence one term at a time.
- A local constraint limits how consecutive choices interact, such as no two adjacent letters being equal.
- You are counting paths, tilings, or arrangements on a grid or a line.
- The answer for size looks like it should depend on the answer for size (and maybe ).
When you notice these, define a state, ask what the last move could have been, and turn that into a transition.
Practice Problems
| Status | Source | Problem Name | Difficulty | Tags | ||
|---|---|---|---|---|---|---|
| Mastering AMC 8 | Medium | Show TagsRecursion | ||||
| Mastering AMC 8 | Medium | Show TagsRecursion | ||||
Problem 1. A frog can jump 1 or 2 lily pads at a time. How many different ways can it reach the 6th lily pad from the start?
Problem 2. A sequence starts with 1, 1, and each new term is the sum of the two before it. What is the 9th term?
Problem 3. How many ways can you tile a grid using only dominoes placed horizontally or vertically?
Problem 4. You are climbing stairs and can take 1, 2, or 3 steps at a time. How many ways can you reach step 7?
Problem 5. A bug walks on a number line starting at 0. Each second it moves or . How many paths of length 6 return the bug to 0 at the end?
Problem 6. How many strings of length 8 made of only A and B contain no two consecutive B's?
Problem 7. On a grid of unit squares, how many paths go from the bottom-left corner to the top-right corner using only unit moves right and up, if the lattice point is blocked and cannot be visited?
Problem 8. A staircase has 8 steps, but step 5 is cracked and cannot be landed on. You may still pass over it with a 2-step. Climbing 1 or 2 steps at a time, how many ways can you reach step 8?
Module Progress:
Join the Discord Community!
Stuck on a problem, or don't understand a module? Join the Discord and get help with your doubts while making more math friends.
