PrevNext

Overview

When solving combinatorial problems, it may be hard to brute-force or manipulate case-work by using words. By using generating functions, you can translate the words and descriptions into algebraic objects , which can help you easily manipulate them. Even simple applications can simplify counting and recurrence problems.

Key Ideas

  • The coefficient of xnx^n in (1+x+x2+⋯ )k(1+x+x^2+\cdots)^k counts solutions to x1+⋯+xk=nx_1+\cdots+x_k=n with nonnegative integers.
  • Finite options become finite polynomials like (1+x+x2)(1+x+x^2).
  • Multiplying series corresponds to combining choices.

Core Skills

Notation

When you want to extract the xnx^n coefficient of the generating function F(x)F(x), use the notation [xn] F(x)[x^n]\ F(x)

Translate Choices to Factors

Each variable contributes a factor (1+x+x2+⋯ )(1+x+x^2+\cdots) or a finite polynomial depending on constraints.

Extract Coefficients

Use known expansions like (1−x)−k(1-x)^{-k} or binomial coefficients to read off coefficients.

Use the geometric series: 1+x+x2+x3+⋯=11−x1+x+x^2+x^3+\cdots=\frac{1}{1-x}.

Use Truncation

When only low-degree terms matter, ignore higher powers after expansion. For example, when you want the coefficient of the term x3x^3, you can ignore multiplying by x4x^4 as it will only produce powers ≥4\ge 4.

Representations

Represent numbers such as cost or amount in the power. (e.g. an apple that costs 50 is represented as x50x^{50})

Recurrence Formula (ADVANCED)

When given a recurrence formula that is to be solved by using generating functions, use the following method:

  1. Make sure that the values of the variable (e.g. nn) such that the recurrence relation is true, is clearly described.

  2. Give a name to the generating function that you will look for, andwrite out that function in terms of the unknown sequence (e.g., call it A(x)=∑n>0anxnA(x)=\sum_{n > 0} a_nx^n).

  3. Multiply both sides of the recurrence by xnx^n, and sum over all values of nn for which the recurrence holds.

  4. Express both sides of the resulting equation explicitly in terms of your generating function A(x)A(x).

  5. Solve the resulting equation for the unknown generating function A(x)A(x).

  6. If you want an exact formula for the sequence that is defined by the given recurrence relation, then attempt to get such a formula by expanding A(x)A(x) into a power series by any method you can think of. In particular, if A(x)A(x) is a rational function (quotient of two polynomials), then success will result from expanding in partial fractions and then handling each of the resulting terms separately.

Worked Example

Count solutions to a+b+c=5a+b+c=5 with a,b,c≥0a,b,c\ge0.

The generating function is (1+x+x2+⋯ )3=1(1−x)3(1+x+x^2+\cdots)^3=\frac{1}{(1-x)^3}. The coefficient of x5x^5 is (5+3−13−1)=21\binom{5+3-1}{3-1}=21.

(Brute Force Method: Since we only need the x5x^5 term, we can let the 'generating function' as (1+x+x2+x3+x4+x5)3(1+x+x^2+x^3+x^4+x^5)^3 and then brute force expand this. NOT RECOMMENDED)

More Examples

Example 1: Bounded Variables

Count solutions to x+y+z=4x+y+z=4 with 0≤x,y,z≤20\le x,y,z\le 2.

Use (1+x+x2)3(1+x+x^2)^3 and extract the x4x^4 coefficient.

Example 2: Distinct Parts

Count partitions of nn into distinct parts.

Use the generating function ∏k≥1(1+xk)\prod_{k \ge 1}(1+x^k).

Example 3: Counting Word Problem

Alice went to a store and saw three items: an apple (cost: 9 cents), a banana (cost: 14 cents), and an orange (cost: 17 cents). She has exactly 2 dollars and doesn't want any change. In how many ways can she achieve this?

Convert this problem into equations: let xx be the number of apples, yy be the number of bananas, zz be the number of oranges. Then x,y,z≥0x, y, z \ge 0. So constraints is 9x+14y+17z=2009x + 14y + 17z = 200. So we wish to find the x200x^{200} coefficient of

∑i=0∞(x9+x14+x17)i\sum^{\infty}_{i=0} (x^9+x^{14}+x^{17})^i

Example 4: Recurrence Extraction (ADVANCED)

Find the general term of fn+1=fn+fn−1f_{n+1}=f_n+f_{n-1} where n≥1,f0=0,f1=1n \ge 1, f_0 = 0, f_1 = 1

By using the above method, we let F(x)=∑n≥0fnxnF(x) = \sum_{n \ge 0} f_nx^n By manipulating the given recurrence relation (multiply by xnx^n and sum over n≥1n \ge 1),

f2x1+f3x2+f4x3+⋯=(f1x1+f2x2+f3x3+⋯ )+(f0x1+f1x2+f2x3+⋯ )f_2x^1+f_3x^2+f_4x^3+\cdots = (f_1x^1+f_2x^2+f_3x^3+\cdots) + (f_0x^1+f_1x^2+f_2x^3+\cdots)

Remember that f0=0f_0 = 0 and f1=1f_1 = 1.

Then,

F(x)−xx=F(x)+xF(x)\frac{F(x)-x}{x} = F(x) + xF(x)

By manipulating this, we get:

F(x)=x1−x−x2F(x)=\frac{x}{1-x-x^2}

By doing partial fractions, we can let this be:

F(x)=x(αx−1)(βx−1)=1α−β(1βx−1−1αx−1)F(x)=\frac{x}{(\alpha x -1 )(\beta x - 1)} = \frac{1}{\alpha-\beta} (\frac{1}{\beta x - 1}-\frac{1}{\alpha x- 1})
=1α−β(1αx−1−1βx−1)= \frac{1}{\alpha-\beta} (\frac{1}{\alpha x - 1}-\frac{1}{\beta x- 1})

Where α=1+52\alpha = \frac{1+\sqrt{5}}{2} and β=1−52\beta = \frac{1-\sqrt{5}}{2} (Notice that α=ϕ\alpha = \phi, the golden ratio). Now, using the geometric series 11−x=1+x+x2+x3+⋯\frac{1}{1-x} = 1+x+x^2+x^3+\cdots.

[xn] F(x)=1α−β(αn−βn)[x^n]\ F(x) = \frac{1}{\alpha-\beta} (\alpha^n - \beta^n)

By substituting the values of α\alpha and β\beta,

[xn] F(x)=15((1+52)n−(1−52)n)[x^n]\ F(x) = \frac{1}{\sqrt{5}} ((\frac{1+\sqrt{5}}{2})^n - (\frac{1-\sqrt{5}}{2})^n)

When comparing this to Binet's formula for Fibonacci numbers, this is equal to

fn=15((1+52)n−(1−52)n)f_n = \frac{1}{\sqrt{5}} ((\frac{1+\sqrt{5}}{2})^n - (\frac{1-\sqrt{5}}{2})^n)

Example 5: Interesting Sum

Your goal is to evaluate the following expression:

Sn=∑k=0n(n−kk)2k.S_n=\sum_{k=0}^{n} \binom{n-k}{k}2^k.

First, we let

F(x)=∑n≥0Snxn.F(x)=\sum_{n\ge0}S_nx^n.

Then

F(x)=∑n≥0∑k≥0(n−kk)2kxn.F(x)=\sum_{n\ge0}\sum_{k\ge0}\binom{n-k}{k}2^kx^n.

Using

∑m≥k(mk)xm=xk(1−x)k+1,\sum_{m\ge k}\binom{m}{k}x^m=\frac{x^k}{(1-x)^{k+1}},

we get that

F(x)=11−x∑k≥0(2x21−x)k=11−x−2x2.F(x)=\frac{1}{1-x}\sum_{k\ge0}\left(\frac{2x^2}{1-x}\right)^k =\frac{1}{1-x-2x^2}.

And factoring,

1−x−2x2=(1−2x)(1+x).1-x-2x^2=(1-2x)(1+x).

Then, we see that partial fractions gives us the following:

F(x)=2/31−2x+1/31+x.F(x)=\frac{2/3}{1-2x}+\frac{1/3}{1+x}.

And consequently,

Sn=23⋅2n+13(−1)n=2n+1+(−1)n3.S_n=\frac{2}{3}\cdot2^n+\frac{1}{3}(-1)^n =\frac{2^{n+1}+(-1)^n}{3}.

Example 6: Classic Gen. Functions Problem (remember this!)

Let ana_n denote the number of ordered compositions of nn whose parts are only 1,2,1,2, or 44. Your job is to find ana_n.

First, notice that each part contributes the following:

x+x2+x4.x+x^2+x^4.

Thus, we have that

A(x)=1+(x+x2+x4)+(x+x2+x4)2+⋯=11−x−x2−x4.A(x)=1+(x+x^2+x^4)+(x+x^2+x^4)^2+\cdots =\frac{1}{1-x-x^2-x^4}.

Next, we can multiply through to get:

(1−x−x2−x4)A(x)=1.\left(1-x-x^2-x^4\right)A(x)=1.

And comparing the coefficients gives us our answer:

an=an−1+an−2+an−4,n≥4.a_n=a_{n-1}+a_{n-2}+a_{n-4},\qquad n\ge4.

Example 7: Only Even-Sized Subsets (also very common! Remember this!)

How many subsets of {1,2,…,n}\{1,2,\dots,n\} contain an even number of elements (solve using generating functions)?

First, notice that the generating function is simply:

(1+x)n=∑k=0n(nk)xk.(1+x)^n=\sum_{k=0}^{n}\binom{n}{k}x^k.

Realize that the sum of the even coefficients is

(1+1)n+(1−1)n2.\frac{(1+1)^n+(1-1)^n}{2}.

And consequently, we get

2n+02=2n−1.\frac{2^n+0}{2}=2^{n-1}.

Which makes the answer:

2n−1.2^{n-1}.

Example 8: More binomial expressions!

Your job is to show that the following expression is true:

∑k=0n(2kk)(2n−2kn−k)=4n.\sum_{k=0}^{n}\binom{2k}{k}\binom{2n-2k}{n-k}=4^n.

First, recall that

∑n≥0(2nn)xn=11−4x.\sum_{n\ge0}\binom{2n}{n}x^n=\frac{1}{\sqrt{1-4x}}.

Then, squaring the expression, we get:

(∑n≥0(2nn)xn)2=11−4x.\left(\sum_{n\ge0}\binom{2n}{n}x^n\right)^2 =\frac{1}{1-4x}.

Notice that the coefficient of xnx^n on the left is simply

∑k=0n(2kk)(2n−2kn−k),\sum_{k=0}^{n}\binom{2k}{k}\binom{2n-2k}{n-k},

while the coefficient on the right is

4n.4^n.

Thus, we have:

∑k=0n(2kk)(2n−2kn−k)=4n.\sum_{k=0}^{n}\binom{2k}{k}\binom{2n-2k}{n-k}=4^n.

Example 9: Catalan Numbers (very famous and fun numbers with many applications!)

Your goal is to find a generating function for the Catalan numbers CnC_n, which are defined by

C0=1,Cn=∑k=0n−1CkCn−1−k(n≥1).C_0=1,\qquad C_n=\sum_{k=0}^{n-1}C_kC_{n-1-k} \quad (n\ge1).

First, we let

C(x)=∑n≥0Cnxn.C(x)=\sum_{n\ge0}C_nx^n.

Then, using the recurrence relation, we get that

C(x)=1+xC(x)2.C(x)=1+xC(x)^2.

Solving the quadratic equation,

C(x)=1±1−4x2x.C(x)=\frac{1\pm\sqrt{1-4x}}{2x}.

Since C(0)=1C(0)=1, we can simply choose the negative one

Thus,

C(x)=1−1−4x2x.C(x)=\frac{1-\sqrt{1-4x}}{2x}.

Example 10: Derangements

Let dnd_n denote the number of permutations of {1,2,…,n}\{1,2,\dots,n\} with no fixed points.

First, notice that the exponential generating function for all permutations is

∑n≥0n!xnn!=11−x.\sum_{n\ge0}n!\frac{x^n}{n!}=\frac{1}{1-x}.

Next, notice that for each element, either it is fixed (contributing xx) or not fixed (contributing 11), so we can simply remove fixed points by multiplying by e−xe^{-x}.

Hence the exponential generating function for derangements is

D(x)=e−x1−x.D(x)=\frac{e^{-x}}{1-x}.

Therefore,

D(x)=∑n≥0dnxnn!=e−x1−x.D(x)=\sum_{n\ge0}d_n\frac{x^n}{n!}=\frac{e^{-x}}{1-x}.

Strategy Checklist

  • Translate each constraint into a factor.
  • Use binomial series for (1−x)−k(1-x)^{-k}.
    • Use the geometric series 1+x+x2+⋯=11−x1+x+x^2+\dots = \frac{1}{1-x}.
  • Truncate when only a few coefficients are needed.
  • Convert the amount of an item into a power of xx.
  • Use the above 'method' to solve for the recurrence relation.
    1. Describe variables
    2. Name Generating Functions
    3. Sum over all nn that the relation holds
    4. Express equation in generating functions from Step 2
    5. Solve equation
    6. Expand generating function into power series to extract coefficients.

(This module was based on generating functionology: https://www2.math.upenn.edu/~wilf/gfologyLinked2.pdf )

Practice Problems

StatusSourceProblem NameDifficultyTags
MathNetHard
Show TagsGenerating functions, Sums and products
Harvard-MIT Math TournamentHard
Show TagsGenerating functions, Recurrence relations
USA IMOHard
Show TagsGenerating functions, Sums and products, Telescoping series
USA IMOHard
Show TagsCounting two ways, Generating functions, Modular Arithmetic, Pigeonhole principle
Harvard-MIT Math TournamentHard
Show TagsGenerating functions, Sums and products
Harvard-MIT Mathematics TournamentHard
Show TagsGenerating functions, Recurrence relations
USA IMOHard
Show TagsComplex numbers, Enumeration with symmetry, Generating functions, Inclusion-exclusion, Roots of unity
Harvard-MIT Mathematics TournamentHard
Show TagsGenerating functions, Polynomial operations
Harvard-MIT Mathematics TournamentHard
Show TagsGenerating functions, Roots of unity
Harvard-MIT Mathematics TournamentHard
Show TagsGenerating functions, Recurrence relations, Sums and products
Harvard-MIT Mathematics TournamentHard
Show TagsGenerating functions
10th Annual Harvard-MIT Mathematics TournamentHard
Show TagsCounting two ways, Generating functions
11th Annual Harvard-MIT Mathematics TournamentHard
Show TagsGenerating functions
11th Annual Harvard-MIT Mathematics TournamentHard
Show TagsCatalan numbers, partitions, Generating functions, Sums and products
Harvard-MIT Mathematics TournamentHard
Show TagsGenerating functions
Harvard-MIT Mathematics TournamentHard
Show TagsGenerating functions, Recurrence relations, Sums and products
Team Selection Test 2010Hard
Show TagsCounting two ways, Generating functions, Inclusion-exclusion, Induction / smoothing
Harvard-MIT Mathematics TournamentHard
Show TagsAlgebraic properties of binomial coefficients, Generating functions, Sequences and Series
Team Selection TestHard
Show TagsGenerating functions, Modular Arithmetic, Polynomial operations
HMMTHard
Show TagsFermat / Euler / Wilson theorems, Generating functions, Greatest common divisors (gcd), Multiplicative order, Other
USAMOHard
Show TagsAlgebraic properties of binomial coefficients, Generating functions, Induction / smoothing, Recursion, bijection
HMMT 2013Hard
Show TagsEnumeration with symmetry, Generating functions
HMMTHard
Show TagsGenerating functions, Sums and products
HMMT 2014Hard
Show TagsComplex numbers, Generating functions, Recurrence relations, Roots of unity, φ (Euler's totient)
HMMT February 2016Hard
Show TagsGenerating functions, Polynomial operations, Ring Theory
February 2017Hard
Show TagsExpected values, Generating functions, Sums and products
February 2017Hard
Show TagsGenerating functions, Roots of unity
USA TSTSTHard
Show TagsAlgebraic properties of binomial coefficients, Coloring schemes, extremal arguments, Expected values, Generating functions, Induction / smoothing, Invariants / monovariants, Roots of unity
HMMT November 2019Hard
Show TagsExpected values, Generating functions, Inclusion-exclusion
HMMT February 2019Hard
Show TagsGenerating functions, Other, Prime numbers, Recurrence relations
HMMT February 2020Hard
Show TagsAlgebraic properties of binomial coefficients, Expected values, Generating functions
HMMT February 2020Hard
Show TagsAlgebraic properties of binomial coefficients, Catalan numbers, partitions, Generating functions
HMICHard
Show TagsAlgebraic properties of binomial coefficients, Fermat / Euler / Wilson theorems, Generating functions, Polynomials mod p, Quadratic residues
HMMT Spring 2021 Team RoundHard
Show TagsAlgebraic properties of binomial coefficients, Counting two ways, Generating functions, Induction / smoothing, Recursion, bijection
HMICHard
Show TagsDecimals, Generating functions, Sums and products
AIME IIHard
Show TagsGenerating functions, Polynomials, Techniques: modulo, size analysis, order analysis, inequalities
HMICHard
Show TagsGenerating functions, Induction / smoothing, Sums and products
USA TSTSTHard
Show TagsColoring schemes, extremal arguments, Generating functions
HMMT November 2023Hard
Show TagsCounting two ways, Generating functions, Sums and products
USA TSTHard
Show TagsGenerating functions, Greatest common divisors (gcd), Induction / smoothing, Roots of unity, Vectors

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.

PrevNext