LemmaGuides › Generating functions: turning a counting problem into a product
Guide

Generating functions: turning a counting problem into a product

3 September 2026

Generating functions are usually explained backwards. You are shown 11x=1+x+x2+\frac{1}{1-x} = 1 + x + x^2 + \cdots, 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 xnx^n is the answer for total nn, and xx 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 xnx^n in a product A(x)B(x)A(x)B(x) is kakbnk\sum_k a_k b_{n-k}, 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:

  1. One factor per kind of thing you are choosing.
  2. Exponents record cost. An item that adds ww to the total contributes xwx^w.
  3. Multiply, then read off the coefficient of xnx^n.

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 phrasing to listen for is how many ways to make a given total out of these, using as many of each as you like, or at most one of each, or at most two of each. Each of those clauses is a factor, and the factors are the only thing you have to get right.

The dictionary

For a single item of size ww, the constraint on how many copies you may take translates directly:

One identity is worth memorising because it converts a coefficient extraction into a binomial coefficient on sight: 1(1x)k\frac{1}{(1-x)^k} has coefficient (n+k1k1)\binom{n+k-1}{k-1} at xnx^n. 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 x+x2+x3+x4+x5+x6x + x^2 + x^3 + x^4 + x^5 + x^6. Note the missing constant term: a die must show something, so "zero" is not an option, and the factor starts at x1x^1. Three dice are independent, so the answer is the coefficient of x10x^{10} in (x+x2++x6)3(x + x^2 + \cdots + x^6)^3, which is 2727.

You could also get 2727 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 1,2,2,3,3,41,2,2,3,3,4, 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 1(1x)(1x2)(1x5)\frac{1}{(1-x)(1-x^2)(1-x^5)} 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 xnx^n.

Start with the row for the empty selection: a 11 in position 00 and zeros everywhere else. Then, for each 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 22s gives a row with a 11 in every even position; allowing at most one 22 gives a row with a 11 in positions 00 and 22 and nothing else. Same loop, different source row, completely different object.

Run it on coins of 11, 22 and 55 making 2525. After the 11s every entry is 11. After the 22s the row is 1,1,2,2,3,3,4,4,1,1,2,2,3,3,4,4,\ldots — each amount plus its own count two places back. After the 55s the entry at 2525 is 4242, and that is the answer. Three sweeps of a 2626-entry row, no algebra.

The free sanity check

Take subsets of {1,2,,10}\{1,2,\ldots,10\} and ask how many sum to 2525. Every element is in or out, so the product is (1+x)(1+x2)(1+x10)(1+x)(1+x^2)\cdots(1+x^{10}) and the answer is the coefficient of x25x^{25}, which is 3939.

The coefficient of x30x^{30} is also 3939, and not by accident. The whole set sums to 5555, so complementing a subset turns a sum of 2525 into a sum of 3030. The coefficient list is a palindrome, and the bijection producing it is exactly the complement map that proves (nk)=(nnk)\binom{n}{k} = \binom{n}{n-k}. Whenever your factors are 1+xw1 + x^{w} 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 2102^{10}, which is just the count of subsets of a 1010-element set; the generating function is that count refined by total. And the row update for a factor 1+x1+x, new[n]=old[n]+old[n1]\text{new}[n] = \text{old}[n] + \text{old}[n-1], 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

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