Generating functions: turning a counting problem into a product
3 September 2026
Generating functions are usually explained backwards. You are shown , warned that it does not converge, told the warning does not matter, and then handed a problem. What is missing is the part you actually need in a contest: when to reach for one, what to write down, and how to get a number out without doing partial fractions.
So this is the method, not the definition.
The one sentence that carries everything
A generating function is a filing cabinet. The coefficient of is the answer for total , and is never substituted for anything; it is only there to keep the drawers apart.
The reason this is useful is one fact about multiplication. The coefficient of in a product is , which reads in English as take some from the first pile and some from the second, and add the sizes. That is exactly what independent choices do. So:
- One factor per kind of thing you are choosing.
- Exponents record cost. An item that adds to the total contributes .
- Multiply, then read off the coefficient of .
That is the whole procedure. Everything below is either the dictionary for step 1 or the arithmetic for step 3.
Three signals that a counting problem is this problem
- The total is a sum of weights. Coins summing to an amount, dice summing to a score, part sizes summing to . If the thing being constrained is a total, exponents can carry it.
- The constraint is per item, not global. "Each part may be used at most twice" is a property of one part on its own, so it becomes one factor. "The largest part is twice the smallest" relates two parts and cannot be a factor.
- You want one specific total, but nothing about the setup depends on which total. The product is built once and answers every at the same time, which is why it beats casework the moment the question is asked twice.
The dictionary
For a single item of size , the constraint on how many copies you may take translates directly:
- Any number of copies:
- At most one copy:
- At most two copies:
- A fixed number of copies, of them:
- A bounded number of copies, between and :
- An ordered sequence of parts, drawn from a set whose series is :
One identity is worth memorising because it converts a coefficient extraction into a binomial coefficient on sight: has coefficient at . That is stars and bars wearing algebra, and if your product collapses to that shape you are already finished.
A first one, end to end
In how many ways can three standard dice sum to ten?
One die is . Note the missing constant term: a die must show something, so "zero" is not an option, and the factor starts at . Three dice are independent, so the answer is the coefficient of in , which is .
You could also get by casework, and on this problem casework is faster. The reason to write the product anyway is that it survives a change in the question. Four dice, or dice labelled , or "at least one six" — each is a small edit to a factor, and none of them is a small edit to your casework.
Extraction without partial fractions
Nobody expands by partial fractions on a timed paper. You multiply the factors into a row of coefficients, one factor at a time, and the row is short because you only need up to .
Start with the row for the empty selection: a in position and zeros everywhere else. Then, for each factor:
- Unlimited copies, the factor : sweep upward and set . You read the row you are currently writing.
- At most one copy, the factor : take a copy of the row first, then set . You read the row as it was before this factor.
That distinction is the single mistake people make with this method, and it is invisible once made: sweeping upward with an at-most-one factor silently grants you unlimited copies, and the answer comes out too big with nothing to point at. Test it on one factor. Allowing any number of s gives a row with a in every even position; allowing at most one gives a row with a in positions and and nothing else. Same loop, different source row, completely different object.
Run it on coins of , and making . After the s every entry is . After the s the row is — each amount plus its own count two places back. After the s the entry at is , and that is the answer. Three sweeps of a -entry row, no algebra.
The free sanity check
Take subsets of and ask how many sum to . Every element is in or out, so the product is and the answer is the coefficient of , which is .
The coefficient of is also , and not by accident. The whole set sums to , so complementing a subset turns a sum of into a sum of . The coefficient list is a palindrome, and the bijection producing it is exactly the complement map that proves . Whenever your factors are across a full set, check the two ends of your row against each other before you trust the middle.
Two more things fall out of the same product for free. Ignoring the sums entirely — adding up every coefficient — gives , which is just the count of subsets of a -element set; the generating function is that count refined by total. And the row update for a factor , , is Pascal's identity written as a loop. The method you are running by hand is the identity you already know.
When it is the wrong tool
- The answer is a single binomial coefficient. If the product is , quote stars and bars and stop.
- The constraint relates two parts to each other. "All parts distinct" is fine, because it is per part: . "No two parts differ by exactly one" is not, and no amount of rearranging makes it a factor.
- The objects are labelled and order matters inside a part. That is the territory of exponential generating functions, which use and are a genuinely different tool, not a variation.
- The count is small and structured. Constructive counting with a correction for overcounting is shorter than a product whenever you can describe the object in two or three choices.
What to practise
Do not practise expanding series. Practise the translation. Take twenty counting problems and write only the product for each — factors and the exponent you would extract — then stop and move to the next one. Ten minutes of that buys more than one fully worked example, because the product is the step you will get wrong under time pressure and the arithmetic is the step you will not.
Lemma teaches this as a named technique with the identities and the recognition rules attached: see generating functions for the statement and where it sits, and the technique index for the neighbours it trades against. The problem archive carries the combinatorics problems above in full, and the daily problem posts a fresh one every day in three difficulty tiers with no account needed.
Train it on Lemma
Every technique here is taught explicitly, in order. 125 lessons, 1206 curated problems and unlimited generated practice at six difficulties. Free to start, no card, and every paid plan opens with 3 free days.
Find your level