Math Core

Module 2.6 · Counting and Probability

Generating functions

A generating function stores an entire sequence of counts as the coefficients of one polynomial or power series. The payoff is that combining choices becomes multiplying polynomials, and questions like "how many ways to reach a total of nn" become "what is the coefficient of xnx^n?"

Encoding choices as polynomials

Suppose one choice contributes an amount aa in cac_a ways. Record it as the polynomial ∑caxa\sum c_a x^a. When you make several independent choices and add the amounts, the exponents add when you multiply, and each way of getting a total shows up exactly once in the product.

Definition

Generating function

The generating function of a sequence c0,c1,c2,…c_0, c_1, c_2, \dots is c0+c1x+c2x2+…c_0 + c_1 x + c_2 x^2 + \dots. The coefficient of xnx^n is written [xn][x^n] of the function.

Products count combinations

If choice 11 has generating function A(x)A(x) and choice 22 has generating function B(x)B(x), and the totals add, then the number of ways to reach total nn is [xn] A(x)B(x)[x^n]\,A(x)B(x).

A fair die is x+x2+x3+x4+x5+x6x + x^2 + x^3 + x^4 + x^5 + x^6. Rolling three dice is (x+x2+⋯+x6)3(x + x^2 + \dots + x^6)^3, and the coefficient of x10x^{10} counts the outcomes with sum 1010.

Useful series

  • 11−x=1+x+x2+…\dfrac{1}{1 - x} = 1 + x + x^2 + \dots (any number of an item, each worth 11).
  • 11−xk=1+xk+x2k+…\dfrac{1}{1 - x^k} = 1 + x^k + x^{2k} + \dots (any number of an item worth kk).
  • 1+x+⋯+xm=1−xm+11−x1 + x + \dots + x^m = \dfrac{1 - x^{m+1}}{1 - x} (at most mm of an item).
  • 1(1−x)k=∑n≥0(n+k−1k−1)xn\dfrac{1}{(1 - x)^k} = \displaystyle\sum_{n \ge 0} \binom{n + k - 1}{k - 1} x^n, which is stars and bars in disguise.

The last two together give a fast way to extract dice coefficients:

(x+⋯+x6)3=x3(1−x6)3(1−x)3.(x + \dots + x^6)^3 = x^3 \frac{(1 - x^6)^3}{(1 - x)^3}.

Evaluating at special points

Plugging in numbers gives sums of coefficients. For f(x)=∑cnxnf(x) = \sum c_n x^n:

  • f(1)f(1) is the sum of all coefficients.
  • f(1)+f(−1)2\dfrac{f(1) + f(-1)}{2} is the sum of the coefficients of even powers.
  • With ω\omega a primitive kkth root of unity, the sum of cnc_n over nn divisible by kk is 1k∑j=0k−1f(ωj)\dfrac{1}{k}\displaystyle\sum_{j=0}^{k-1} f(\omega^j). This is the roots of unity filter.

The filter works because 1+ωn+ω2n+⋯+ω(k−1)n1 + \omega^n + \omega^{2n} + \dots + \omega^{(k-1)n} equals kk when kk divides nn and 00 otherwise.

Common mistake

Match each factor to one choice, with the right exponents. A coin worth 55 cents is 11−x5\dfrac{1}{1 - x^5}, not 51−x\dfrac{5}{1 - x}. Writing a factor for the wrong range (for example, allowing 00 when a die can't show 00) shifts every coefficient.

Worked example: Three dice

In how many of the 216216 outcomes of rolling three dice is the sum 1010?

We want [x10] x3(1−x6)3(1−x)−3=[x7] (1−3x6+… )(1−x)−3[x^{10}]\,x^3 (1 - x^6)^3 (1 - x)^{-3} = [x^7]\,(1 - 3x^6 + \dots)(1 - x)^{-3}. That is

(92)−3(32)=36−9=27.\binom{9}{2} - 3\binom{3}{2} = 36 - 9 = 27.

Worked example: Making change

In how many ways can you make 1010 cents from 11-cent, 22-cent, and 55-cent coins?

The generating function is 1(1−x)(1−x2)(1−x5)\dfrac{1}{(1 - x)(1 - x^2)(1 - x^5)}. Extracting [x10][x^{10}] by the number of 55-cent coins: with none, 1010 cents from 11s and 22s can use 00 to 55 twos (66 ways); with one, 55 cents uses 00 to 22 twos (33 ways); with two, 11 way. The total is 1010.

Worked example: A roots of unity filter

How many subsets of {1,2,…,10}\{1, 2, \dots, 10\} (including the empty set) have a sum divisible by 55?

The generating function is f(x)=(1+x)(1+x2)⋯(1+x10)f(x) = (1 + x)(1 + x^2) \cdots (1 + x^{10}), and we want the sum of the coefficients of xnx^n with 5∣n5 \mid n. Let ω\omega be a primitive 55th root of unity. For j=1,2,3,4j = 1, 2, 3, 4, the numbers ωj,ω2j,…,ω10j\omega^{j}, \omega^{2j}, \dots, \omega^{10j} run through each 55th root of unity exactly twice, so

f(ωj)=(∏r=04(1+ωr))2.f(\omega^j) = \Big(\prod_{r=0}^{4} (1 + \omega^r)\Big)^2.

Since ∏r=04(z−ωr)=z5−1\prod_{r=0}^{4} (z - \omega^r) = z^5 - 1, putting z=−1z = -1 gives ∏(−1−ωr)=−2\prod (-1 - \omega^r) = -2, so ∏(1+ωr)=2\prod (1 + \omega^r) = 2. Hence f(ωj)=4f(\omega^j) = 4, and the count is

f(1)+4⋅45=1024+165=208.\frac{f(1) + 4 \cdot 4}{5} = \frac{1024 + 16}{5} = 208.

Tip

Euler's identity ∏k≥1(1+xk)=∏k≥111−x2k−1\displaystyle\prod_{k \ge 1}(1 + x^k) = \prod_{k \ge 1}\frac{1}{1 - x^{2k-1}} says that the number of partitions of nn into distinct parts equals the number of partitions into odd parts. It follows from 1+xk=1−x2k1−xk1 + x^k = \dfrac{1 - x^{2k}}{1 - x^k} and cancelling.

Practice

Practice 1

Twelve identical candies are shared among Ann, Bea, and Cy. Ann must get an even number, Bea at most 44, and Cy a multiple of 33 (zero counts as even and as a multiple of 33). In how many ways can the candies be shared?

Enter a number. Fractions like 3/4 and sqrt(2) are OK.

Practice 2

Four fair dice are rolled. In how many of the 646^4 outcomes is the sum 1212?

Enter a number. Fractions like 3/4 and sqrt(2) are OK.

Practice 3

In how many ways can you make one dollar using nickels (55 cents), dimes (1010 cents), and quarters (2525 cents)?

Enter a number. Fractions like 3/4 and sqrt(2) are OK.

Practice 4

Find the number of ordered triples (a,b,c)(a, b, c) of nonnegative integers with a+2b+3c=20a + 2b + 3c = 20.

Enter a number. Fractions like 3/4 and sqrt(2) are OK.

Practice 5

When (1+x+x2)6(1 + x + x^2)^6 is expanded, what is the sum of the coefficients of the terms xkx^k with kk divisible by 33?

Enter a number. Fractions like 3/4 and sqrt(2) are OK.

Practice 6

In how many ways can 2020 be written as a sum of distinct positive integers, where order does not matter? (For example, 2020 itself counts, and 9+7+49 + 7 + 4 counts once.)

Enter a number. Fractions like 3/4 and sqrt(2) are OK.

Practice 7

Let NN be the number of subsets of {1,2,3,…,15}\{1, 2, 3, \dots, 15\} (including the empty set) whose elements have a sum divisible by 55. Find the remainder when NN is divided by 10001000.

Enter a number. Fractions like 3/4 and sqrt(2) are OK.

Real contest practice

  • 2010 AIME I, Problem 4: two players flip a set of coins; multiply the coins' generating functions to get the head counts.
  • 2016 AIME II, Problem 6: a sum of absolute values of coefficients, found by evaluating a product of polynomials at a single point.
  • 2018 AIME I, Problem 12: subset sums modulo 33, a natural fit for the roots of unity filter.