Chapter

Generating Functions I

The module introduces generating functions as coefficient language for counting sums, bounded compositions, weighted subsets, partitions, and recurrences.
Log in to track solved progress and bookmarks.

Theory

Key Idea

A generating function turns a counting problem into a coefficient problem. If the number of ways to obtain sum \(n\) is \(a_n\), we write \(A(x)=a_0+a_1x+a_2x^2+\cdots\). Then the answer to "how many ways produce \(n\)" is the coefficient of \(x^n\).

The power of the method is that multiplication automatically combines independent choices. One factor describes one object or one restriction, and the product describes all objects at once.

Basic Facts

  • The coefficient of \(x^n\) is denoted by \([x^n]A(x)\).
  • \((1+x)^m\) encodes choosing a subset of \(m\) elements: \(x\) means "chosen", \(1\) means "not chosen".
  • \(1+x+x^2+\cdots=\frac{1}{1-x}\) encodes an unlimited number of identical objects.
  • \(1+x+\cdots+x^r\) encodes choosing a number from \(0\) to \(r\).
  • The product \(A(x)B(x)\) corresponds to independently adding contributions: coefficients are summed over all splits of the total.

When to Use This Method

  • You need to count solutions of \(a_1+\cdots+a_k=n\) with restrictions.
  • You are choosing subsets with a prescribed sum of weights.
  • A recurrence appears, especially a Fibonacci-type recurrence.
  • You need to count partitions, compositions, or tilings of a strip.
  • Direct casework grows quickly, but every local choice is simple.

How to Recognise the Method

Look for the question "what total weight was obtained?" If each object contributes a small amount to the exponent of \(x\), then a product of factors often gives the whole count.

Another sign is wording such as "at most", "exactly", "sum equals", or "how many times may be taken". These restrictions usually translate into finite or infinite geometric sums.

Typical Mistakes

  • Confusing ordered compositions with unordered partitions.
  • Using an infinite factor when the statement has an upper bound.
  • Forgetting the shift when a variable must be positive rather than nonnegative.
  • Extracting the coefficient of the wrong power after a change of variables.
  • Expanding long products by hand instead of using symmetry, inclusion-exclusion, or a recurrence.

Mini-Checklist

  • What contributes to the exponent of \(x\)?
  • Is one choice independent of another, or does order matter?
  • Is the factor finite or infinite?
  • Which coefficient must be extracted?
  • Can the coefficient be simplified by symmetry, the substitution \(x\mapsto -x\), a recurrence, or inclusion-exclusion?

Examples

Example 1. A Coefficient as a Choice

We start with the essential meaning of a coefficient: each factor represents one independent choice.

Problem. Find the coefficient of \(x^3\) in \((1+x)^7\).

Solution.

When expanding \((1+x)^7\), we choose either \(1\) or \(x\) from each of the \(7\) factors. To obtain \(x^3\), we must choose \(x\) from exactly three factors. This can be done in \(\binom{7}{3}=35\) ways.

Key idea. The coefficient is the number of ways to choose the positions from which the factor \(x\) came.

Example 2. Unrestricted Nonnegative Solutions

A geometric series encodes a variable that can take any nonnegative value.

Problem. How many nonnegative integer solutions does \(a+b+c=12\) have?

Solution.

Each variable gives the factor \(1+x+x^2+\cdots=\frac{1}{1-x}\). We need \([x^{12}]\frac{1}{(1-x)^3}\).

Since \(\frac{1}{(1-x)^3}=\sum_{n\ge 0}\binom{n+2}{2}x^n\), the answer is \(\binom{14}{2}=91\).

Key idea. This is the stars-and-bars method written in coefficient language.

Example 3. An Upper Bound

When a variable is bounded above, the infinite series becomes a finite polynomial.

Problem. How many solutions does \(a+b+c=8\) have if \(0\le a,b,c\le 3\)?

Solution.

We need the coefficient of \(x^8\) in \((1+x+x^2+x^3)^3\). It is convenient to use the complement:

\((1+x+x^2+x^3)^3=\left(\frac{1-x^4}{1-x}\right)^3\).

Without the upper bound, there are \(\binom{10}{2}=45\) solutions. If, for example, \(a\ge 4\), then after setting \(a'=a-4\) we get \(a'+b+c=4\), giving \(\binom{6}{2}=15\) solutions. There are three variables. If two variables are at least \(4\), the remaining sum is \(0\), giving \(\binom{3}{2}=3\) cases. Therefore the answer is \(45-3\cdot 15+3=3\).

Key idea. Upper bounds often lead to inclusion-exclusion.

Example 4. Subset Sum by Weights

The product \(\prod(1+x^{w_i})\) encodes choosing or not choosing objects with weights \(w_i\).

Problem. How many subsets of \(\{2,3,5,7\}\) have sum \(10\)?

Solution.

We need the coefficient of \(x^{10}\) in

\[(1+x^2)(1+x^3)(1+x^5)(1+x^7).\]

The sum \(10\) is obtained in two ways: \(3+7\) and \(2+3+5\). Hence the coefficient is \(2\).

Key idea. Each element can be taken at most once, so its factor is \(1+x^{w_i}\).

Example 5. A Fibonacci Recurrence

A generating function can also derive a recurrence, not just extract a coefficient.

Problem. Let \(t_n\) be the number of tilings of a \(1\times n\) strip by \(1\times 1\) squares and \(1\times 2\) dominoes. Find the generating function.

Solution.

A tiling starts either with one square, leaving a \(1\times(n-1)\) strip, or with one domino, leaving a \(1\times(n-2)\) strip. Thus \(t_n=t_{n-1}+t_{n-2}\), with \(t_0=1\), \(t_1=1\).

For \(T(x)=\sum_{n\ge 0}t_nx^n\), we get \(T(x)=1+xT(x)+x^2T(x)\). Therefore

\[T(x)=\frac{1}{1-x-x^2}.\]

Key idea. First build the first step, then translate the recurrence into an equation for the series.

Example 6. A Parity Filter

Substituting \(x=1\) sums all coefficients, while substituting \(x=-1\) subtracts odd-degree coefficients.

Problem. How many subsets of an \(n\)-element set have even size?

Solution.

The coefficients of \((1+x)^n\) count subsets by size. The sum of even coefficients is

\[\frac{(1+1)^n+(1-1)^n}{2}.\]

For \(n\ge 1\), this equals \(2^{n-1}\).

Key idea. This is the first filtering idea: special substitutions separate the desired degrees.

Example 7. Distinct Parts and Odd Parts

Sometimes a generating function proves equality between two very different descriptions.

Problem. Show that the number of partitions of \(n\) into distinct parts equals the number of partitions of \(n\) into odd parts.

Solution.

Distinct parts are encoded by \(\prod_{i\ge 1}(1+x^i)\). Odd parts are encoded by \(\prod_{j\ge 1}\frac{1}{1-x^{2j-1}}\).

Now

\[\prod_{i\ge 1}(1+x^i)=\prod_{i\ge 1}\frac{1-x^{2i}}{1-x^i}=\frac{\prod_{i\ge 1}(1-x^{2i})}{\prod_{i\ge 1}(1-x^i)}=\prod_{j\ge 1}\frac{1}{1-x^{2j-1}}.\]

The coefficients of \(x^n\) are equal, so the two numbers of partitions are equal.

Key idea. For the coefficient of \(x^n\), only finitely many factors up to \(i=n\) are actually relevant.

Example 8. Carries in Binary Encoding

In stronger problems, a generating function may hide carries between binary digits.

Problem. Find the coefficient of \(x^7\) in \((1+x+x^2+x^3)(1+x^2+x^4+x^6)(1+x^4+x^8+x^{12})\).

Solution.

The coefficient counts choices \(a_0,a_1,a_2\in\{0,1,2,3\}\) such that \(a_0+2a_1+4a_2=7\).

The number \(7\) has binary form \(111_2\). Count carries. In the lowest digit, \(a_0\) must be odd: \(a_0=1\) gives no carry and \(a_0=3\) gives a carry. In each following digit, the no-carry and carry states again give two transitions, but at the end the carry must be zero. This gives \(4\) ways.

Indeed, the solutions are \((3,2,0)\), \((1,3,0)\), \((3,0,1)\), \((1,1,1)\).

Key idea. Do not expand the whole product: in such problems, carries are usually cleaner.

Problems

Problems

#9.1
#9.1

Coefficient and Subset Choice

Binomial Coefficients Grade 8 Grade 9 ★★☆☆☆

Find the coefficient of \(x^4\) in \((1+x)^9\), and explain what combinatorial quantity it counts.

Details
Problem: COM-B2-M09-P001
Difficulty: Level 2 of 5
Tag: Binomial Coefficients
Grade: Grade 8, Grade 9
#9.2
#9.2

Three Unrestricted Variables

Counting Grade 8 Grade 9 ★★☆☆☆

How many triples of nonnegative integers \((a,b,c)\) satisfy \(a+b+c=14\)? Solve the problem using a generating function.

Details
Problem: COM-B2-M09-P002
Difficulty: Level 2 of 5
Tag: Counting
Grade: Grade 8, Grade 9
#9.3
#9.3

Bounded Triples

Inclusion-exclusion Grade 8 Grade 9 ★★☆☆☆

Find the number of triples \((a,b,c)\) such that \(a+b+c=9\) and \(0\le a,b,c\le 4\).

Details
Problem: COM-B2-M09-P003
Difficulty: Level 2 of 5
Tag: Inclusion-exclusion
Grade: Grade 8, Grade 9
#9.4
#9.4

Sum of Chosen Numbers

Generating Functions Grade 8 Grade 9 ★★☆☆☆

How many subsets of \(\{1,2,3,4,5,6,7\}\) have sum of elements equal to \(8\)?

Details
Problem: COM-B2-M09-P004
Difficulty: Level 2 of 5
Tag: Generating Functions
Grade: Grade 8, Grade 9
#9.5
#9.5

Vandermonde's Identity

Binomial Coefficients Grade 9 Grade 10 ★★☆☆☆

Prove using generating functions that for nonnegative integers \(r,s,n\),

\[\sum_{k=0}^{n}\binom{r}{k}\binom{s}{n-k}=\binom{r+s}{n}.\]

Details
Problem: COM-B2-M09-P005
Difficulty: Level 2 of 5
Tag: Binomial Coefficients
Grade: Grade 9, Grade 10
#9.6
#9.6

Even Summands

Parity Grade 9 Grade 10 ★★★☆☆

How many quadruples of nonnegative even integers \((a,b,c,d)\) satisfy \(a+b+c+d=18\)?

Details
Problem: COM-B2-M09-P006
Difficulty: Level 3 of 5
Tag: Parity
Grade: Grade 9, Grade 10
#9.7
#9.7

Strings with an Even Number of Ones

Parity Grade 9 Grade 10 ★★★☆☆

Prove that for \(n\ge 1\), the number of binary strings of length \(n\) with an even number of ones is \(2^{n-1}\).

Details
Problem: COM-B2-M09-P007
Difficulty: Level 3 of 5
Tag: Parity
Grade: Grade 9, Grade 10
#9.8
#9.8

Compositions Using Ones and Twos

Recursion Grade 9 Grade 10 ★★★☆☆

Let \(c_n\) be the number of ordered representations of \(n\) as a sum of parts \(1\) and \(2\). Find the generating function \(C(x)=\sum_{n\ge 0}c_nx^n\) and express \(c_n\) using Fibonacci numbers.

Details
Problem: COM-B2-M09-P008
Difficulty: Level 3 of 5
Tag: Recursion
Grade: Grade 9, Grade 10
#9.9
#9.9

Five Boxes with an Upper Bound

Inclusion-exclusion Grade 9 Grade 10 ★★★☆☆

In how many ways can \(20\) identical tokens be distributed among \(5\) boxes if each box may contain at most \(6\) tokens?

Details
Problem: COM-B2-M09-P009
Difficulty: Level 3 of 5
Tag: Inclusion-exclusion
Grade: Grade 9, Grade 10
#9.10
#9.10

Coefficient of a Rational Function

Counting Grade 9 Grade 10 ★★★☆☆

Find the coefficient of \(x^{10}\) in \(\frac{1}{(1-x)^2(1-x^3)}\).

Details
Problem: COM-B2-M09-P010
Difficulty: Level 3 of 5
Tag: Counting
Grade: Grade 9, Grade 10
#9.11
#9.11

Distinct Parts of 11

Partitions Grade 9 Grade 10 ★★★☆☆

Find the number of partitions of \(11\) into distinct positive parts.

Details
Problem: COM-B2-M09-P011
Difficulty: Level 3 of 5
Tag: Partitions
Grade: Grade 9, Grade 10
#9.12
#9.12

Symmetry of Coefficients

Generating Functions Grade 9 Grade 10 ★★★☆☆

Let \(c_k\) be the coefficient of \(x^k\) in \((1+x+\cdots+x^m)^n\). Prove that \(c_k=c_{mn-k}\).

Details
Problem: COM-B2-M09-P012
Difficulty: Level 3 of 5
Tag: Generating Functions
Grade: Grade 9, Grade 10
#9.13
#9.13

Six Bounded Summands

Inclusion-exclusion Grade 9 Grade 10 Grade 11 ★★★★☆

Find the number of integer solutions to \(a_1+\cdots+a_6=24\), where \(1\le a_i\le 7\) for all \(i\).

Details
Problem: COM-B2-M09-P013
Difficulty: Level 4 of 5
Tag: Inclusion-exclusion
Grade: Grade 9, Grade 10, Grade 11
#9.14
#9.14

Central Bounded Coefficient

Generating Functions Grade 9 Grade 10 Grade 11 ★★★★☆

Find the coefficient of \(x^{12}\) in \((1+x+x^2+x^3)^8\).

Details
Problem: COM-B2-M09-P014
Difficulty: Level 4 of 5
Tag: Generating Functions
Grade: Grade 9, Grade 10, Grade 11
#9.15
#9.15

Choosing Without Neighbours

Generating Functions Grade 9 Grade 10 Grade 11 ★★★★☆

Prove that the number of ways to choose \(k\) numbers from \(\{1,2,\ldots,n\}\) with no two chosen numbers consecutive is \(\binom{n-k+1}{k}\).

Details
Problem: COM-B2-M09-P015
Difficulty: Level 4 of 5
Tag: Generating Functions
Grade: Grade 9, Grade 10, Grade 11
#9.16
#9.16

Exactly k Dominoes

Recursion Grade 9 Grade 10 Grade 11 ★★★★☆

A \(1\times n\) strip is tiled by \(1\times 1\) squares and \(1\times 2\) dominoes. Prove that the number of tilings with exactly \(k\) dominoes is \(\binom{n-k}{k}\).

Details
Problem: COM-B2-M09-P016
Difficulty: Level 4 of 5
Tag: Recursion
Grade: Grade 9, Grade 10, Grade 11
#9.17
#9.17

Distinct Parts and Odd Parts

Partitions Grade 10 Grade 11 ★★★★☆

Prove that for every \(n\), the number of partitions of \(n\) into distinct parts equals the number of partitions of \(n\) into odd parts.

Details
Problem: COM-B2-M09-P017
Difficulty: Level 4 of 5
Tag: Partitions
Grade: Grade 10, Grade 11
#9.18
#9.18

A Sum with a Parity Condition

Parity Grade 10 Grade 11 ★★★★★

Find the number of 8-tuples \((x_1,\ldots,x_8)\) such that \(0\le x_i\le 5\), \(x_1+\cdots+x_8=30\), and \(x_1+x_2+x_3\) is even.

Details
Problem: COM-B2-M09-P018
Difficulty: Level 5 of 5
Tag: Parity
Grade: Grade 10, Grade 11
#9.19
#9.19

Subset Sum Modulo 3

Generating Functions Grade 10 Grade 11 ★★★★★

Let \(m\ge 1\). Prove that the number of subsets of \(\{1,2,\ldots,3m\}\) whose sum of elements is divisible by \(3\) equals

\[\frac{2^{3m}+2^{m+1}}{3}.\]

Details
Problem: COM-B2-M09-P019
Difficulty: Level 5 of 5
Tag: Generating Functions
Grade: Grade 10, Grade 11
#9.20
#9.20

Binary Weights with Carries

Generating Functions Grade 10 Grade 11 ★★★★★

Let \(m\ge 1\). There are weights \(2^0,2^1,\ldots,2^{m-1}\), and each weight may be taken \(0\), \(1\), \(2\), or \(3\) times. In how many ways can one obtain total weight \(2^m-1\)?

Details
Problem: COM-B2-M09-P020
Difficulty: Level 5 of 5
Tag: Generating Functions
Grade: Grade 10, Grade 11
Source: Method inspiration: local combinatorics source

Ladders

No published ladders were found.
Previous Chapter