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 " become "what is the coefficient of ?"
Encoding choices as polynomials
Suppose one choice contributes an amount in ways. Record it as the polynomial . 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 is . The coefficient of is written of the function.
Products count combinations
If choice has generating function and choice has generating function , and the totals add, then the number of ways to reach total is .
A fair die is . Rolling three dice is , and the coefficient of counts the outcomes with sum .
Useful series
- (any number of an item, each worth ).
- (any number of an item worth ).
- (at most of an item).
- , which is stars and bars in disguise.
The last two together give a fast way to extract dice coefficients:
Evaluating at special points
Plugging in numbers gives sums of coefficients. For :
- is the sum of all coefficients.
- is the sum of the coefficients of even powers.
- With a primitive th root of unity, the sum of over divisible by is . This is the roots of unity filter.
The filter works because equals when divides and otherwise.
Common mistake
Match each factor to one choice, with the right exponents. A coin worth cents is , not . Writing a factor for the wrong range (for example, allowing when a die can't show ) shifts every coefficient.
Worked example: Three dice
In how many of the outcomes of rolling three dice is the sum ?
We want . That is
Worked example: Making change
In how many ways can you make cents from -cent, -cent, and -cent coins?
The generating function is . Extracting by the number of -cent coins: with none, cents from s and s can use to twos ( ways); with one, cents uses to twos ( ways); with two, way. The total is .
Worked example: A roots of unity filter
How many subsets of (including the empty set) have a sum divisible by ?
The generating function is , and we want the sum of the coefficients of with . Let be a primitive th root of unity. For , the numbers run through each th root of unity exactly twice, so
Since , putting gives , so . Hence , and the count is
Tip
Euler's identity says that the number of partitions of into distinct parts equals the number of partitions into odd parts. It follows from and cancelling.
Practice
Twelve identical candies are shared among Ann, Bea, and Cy. Ann must get an even number, Bea at most , and Cy a multiple of (zero counts as even and as a multiple of ). In how many ways can the candies be shared?
Enter a number. Fractions like 3/4 and sqrt(2) are OK.
Four fair dice are rolled. In how many of the outcomes is the sum ?
Enter a number. Fractions like 3/4 and sqrt(2) are OK.
In how many ways can you make one dollar using nickels ( cents), dimes ( cents), and quarters ( cents)?
Enter a number. Fractions like 3/4 and sqrt(2) are OK.
Find the number of ordered triples of nonnegative integers with .
Enter a number. Fractions like 3/4 and sqrt(2) are OK.
When is expanded, what is the sum of the coefficients of the terms with divisible by ?
Enter a number. Fractions like 3/4 and sqrt(2) are OK.
In how many ways can be written as a sum of distinct positive integers, where order does not matter? (For example, itself counts, and counts once.)
Enter a number. Fractions like 3/4 and sqrt(2) are OK.
Let be the number of subsets of (including the empty set) whose elements have a sum divisible by . Find the remainder when is divided by .
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 , a natural fit for the roots of unity filter.