Discrete MathematicsPigeonhole, Inclusion-Exclusion and Generating Functions4 of 4
Pigeonhole, Inclusion-Exclusion and Generating Functions · 4
Generating Functions for Counting
We need a way to count how many combinations of items add up to a target total when each group of items has its own rules, without listing every combination by hand.
1
Counting with Powers of x
Before introducing generating functions, let's look at how ordinary bracket expansion already pairs up choices from different boxes.
Expanding Brackets to Count Combinations
When we replace each option in a box with xamount taken, multiplying one choice from each of the three fruit boxes adds their exponents (xa⋅xb=xa+b), and combining like terms counts how many combinations produce the same total:
So there are 3 ways to walk away with 2 fruits, even though x is just a placeholder and never takes a numerical value.
2
Encoding Rules as Brackets
Now suppose each box has its own restrictions, such as a minimum, a maximum, or items that only come in pairs, and we want to find the number of ways to make a basket of 3 fruits.
Encoding Rules as Generating Functions
Text solution
Encode each box's allowed counts as exponents of xWe list the valid amounts from Box A (0,1, or 2 apples) and Box B (0,2, or 4 bananas) as powers of x and multiply the two brackets:
G(x)=0, 1, or 2 apples(1+x+x2)0, 2, or 4 bananas(1+x2+x4)
Pair terms whose exponents add up to 3Instead of expanding the entire product, we only match terms from the first bracket with terms from the second that multiply to x3:
x1⋅x2=1x3
So there is 1 way to make a basket of 3 fruits (1 apple and 2 bananas).
Expanding the full product gives the count for every possible basket size n at once, which we call the generating functionG(x) for the sequence a0,a1,a2,…:
G(x)=a0+a1x+a2x2+⋯+ways to geta total of nanxn+…
In this two-box product, every allowed power xa in the first bracket pairs with every allowed power xb in the second, and all products xa+b whose exponents add up to n combine into the xn term.
Multiplying the two brackets pairs every allowed choice from the first box with every allowed choice from the second, and each coefficient counts the pairings that give that total.
3
Compressing Series into Closed Forms
When a box allows an unlimited number of items, or when our target total n is large, expanding brackets term by term is too slow. We want to compress a long or infinite series of powers of x into a single fraction, called a closed form.
Compressing Series into Closed Forms
Multiplying the infinite series S=1+x+x2+x3+… by x shifts every power one step to the right, so subtracting the two lines cancels every term after the leading 1:
S−xSS(1−x)S=(1+x+x2+x3+…)−(x+x2+x3+…)=1=1−x1
When a bracket stops at a cutoff xm, shifting and subtracting in the same way leaves a single uncancelled −xm+1 one step past the cutoff:
(1+x+x2+⋯+xm)(1−x)=1−xm+11+x+x2+⋯+xm=1−x1−xm+1
Whether the series stops at xm or continues forever, multiplying by 1−x pairs each middle term +xk with −xk from the term before it, leaving only 1 at the front and −xm+1 right after a finite cutoff.
Subtracting a copy of the sum shifted one power to the right cancels every middle term, leaving only the leading one and the single shifted term just past the cutoff.
Replacing x in 1−x1 with xm (when exponents jump by m) or with rx (when each coefficient is r times the one before) gives the multiples-of-m and ratio-r forms in this list of our core closed forms:
1+x+x2+x3+…=1−x1(any amount: an=1)1+xm+x2m+x3m+…=1−xm1(multiples of m)1+rx+r2x2+r3x3+…=1−rx1(geometric ratio r: an=rn)1+x+x2+⋯+xm=1−x1−xm+1(at most m)C(k,0)+C(k,1)x+⋯+C(k,k)xk=(1+x)k(Binomial Theorem)
4
Translating Between Sequences and Closed Forms
To solve counting problems with algebra, we need to translate in both directions: compressing a sequence an into a closed-form fraction G(x), and unpacking a fraction G(x) back into a formula for the coefficient an.
Translating Between Sequences and Closed Forms
Text solution
Factor out the first nonzero power to compress a sequence with leading zerosFor 0,0,1,1,1,…, the series starts at x2, so we pull x2 out of the front to leave 1−x1:
G(x)=0+0x+1x2+1x3+1x4+…=x2(1+x+x2+…)=1−xx2
Rewrite +rx as −(−rx) to unpack a sum of fractionsTo find an in G(x)=1−3x1+1+x4, we identify the ratio r in each denominator and add their coefficients of xn:
G(x)an=1−3x1+1−(−1)x4=3n+4(−1)n
5
Counting by Canceling Closed Forms
Now we want to find the number of ways to make a bag of 50 candies from four types (an even number of Red, at most 1 Blue, a multiple of 3 Green, and at most 2 Yellow) by compressing each rule into a fraction before multiplying.
Counting by Canceling Closed Forms
Text solution
Encode each candy rule and compress it into its closed formWe write the bracket for each of the four types and replace it with its compressed fraction:
G(x)=Red: even(1+x2+x4+…)⋅Blue:at most 1(1+x)⋅Green: multiple of 3(1+x3+x6+…)⋅Yellow:at most 2(1+x+x2)=1−x21⋅1−x1−x2⋅1−x31⋅1−x1−x3
Cancel matching factors across numerators and denominatorsMultiplying the fractions cancels both (1−x2) and (1−x3):
G(x)=1−x1⋅1−x1=(1−x)21
Unpack the simplified closed form at n=50Because (1−x)21 is two unrestricted boxes (1+x+x2+…)(1+x+x2+…), there are n+1 ways to split n items between them:
a50=50+1=51
So there are 51 ways to make a bag of 50 candies.
6
Extracting Coefficients When Terms Do Not Cancel
Most of the time, the numerators and denominators do not cancel completely. Suppose we want to distribute 10 identical cookies among 3 kids so that every kid gets at least 1 cookie and at most 4 cookies.
When we compress k brackets, the denominator becomes (1−x)k. Because 1−x1 is a single unrestricted box 1+x+x2+…, the power (1−x)k1 is k unrestricted boxes multiplied together. Picking n items in total from k unrestricted boxes is the Stars and Bars count of placing n stars into k boxes separated by k−1 bars, which is C(n+k−1,k−1).
Unpacking (1−x)k1 (or (1−rx)k1 when x is replaced by rx) gives the coefficient of xn for any k boxes:
Let's use this unpacking rule to find the coefficient of x10 in the 10-cookie problem.
Extracting Coefficients When Terms Do Not Cancel
Text solution
Encode the rule and factor out the minimum requirementEach of the 3 kids gets 1,2,3, or 4 cookies, and factoring x out of each bracket gives 1 cookie to each kid up front:
G(x)=(x+x2+x3+x4)3=x3(1+x+x2+x3)3
Compress the bracket and pull the numerator and denominator apartThe bracket stops at x3, so it compresses into 1−x1−x4:
G(x)=x3(1−x1−x4)3=x3(1−x4)3⋅(1−x)31
Unpack the top and bottom pieces separatelyWe expand (1−x4)3 with the Binomial Theorem and unpack (1−x)31 using Stars and Bars with k=3(k−1=2):
Pair terms whose exponents add up to 10We match each term xd in the first bracket (d≤10) with x10−d in the second bracket, and drop 3x11 and −x15 because their exponents already exceed 10:
So there are 6 ways to distribute the 10 cookies among the 3 kids.
You can also read the count before multiplying x3 into (1−3x4+3x8−x12): once x3 accounts for the 3 required cookies, you just pair the terms of (1−3x4+3x8−x12) with (1−x)31 to make the remaining x7, which gives the same 1⋅C(7+2,2)−3⋅C(3+2,2)=6.
As the target power xn increases, each term of x3−3x7+3x11−x15 starts contributing only when n is large enough for that many kids to go over the 4-cookie limit at once, and for n>12 the included terms cancel to zero, because 3 kids can hold at most 12 cookies.
As the total number of cookies grows, each correction term switches on only when there are enough cookies for that many kids to go over the four-cookie limit at once.
Key concepts
To count combinations with generating functions, start by encoding each group's rule as a bracket of allowed powers of x: a minimum of a items starts at xa (which you can factor out of the front), a maximum of m items stops at xm, and packs of size m use only multiples of m(1+xm+x2m+…).
If you only have two or three short brackets and a small target total, like finding the coefficient of x3 in (1+x+x2)(1+x2+x4), do not compress the brackets into fractions; just pick the pairs of terms from each bracket whose exponents add up to 3 directly.
When the brackets are infinite or the target power n is large, compress each bracket before multiplying:
Key concept
Infinite steps of size m:1+xm+x2m+⋯=1−xm1
Key concept
Finite cutoff at xm:1+x+x2+⋯+xm=1−x1−xm+1 (the top exponent is m+1, one step past the cutoff)
Multiply the fractions and cancel any matching factors across the top and bottom. To unpack the coefficient of xn from what remains:
Key concept
A single geometric denominator 1−rx1 unpacks to an=rn (so 1+2x7=1−(−2)x7 gives an=7(−2)n).
Key concept
A repeated denominator (1−rx)k1 unpacks via Stars and Bars to an=C(n+k−1,k−1)rn (so (1−x)31 gives an=C(n+2,2)).
Key concept
A remaining numerator like (1−x4)3 alongside (1−x)31 expands by the Binomial Theorem into 1−3x4+3x8−x12; pair each term xd(d≤n) with the xn−d term from the bottom and ignore powers larger than n.
7
Practice
1
You are filling a gift bag with 6 items chosen from three trays. From Tray A (notebooks), you must take at least 1 and at most 3. From Tray B (pens), pens come in packs of 2 and you can take at most 4 pens. From Tray C (stickers), stickers come in sheets of 3 and you can take at most 6 stickers. Write the generating function for the number of ways to choose n items, and find the number of ways to make a bag of 6 items total.
Watch out: Tray A requires at least 1 notebook, so its bracket starts at x, not 1. Including 1 by mistake adds the invalid choice 1⋅1⋅x6 and gives the wrong answer 4.
Each tray has only three allowed counts. After writing the three brackets, is it faster to compress them into fractions or to match triplets of exponents that add up to 6?
Encoding the allowed item counts for each tray gives:
G(x)=(x+x2+x3)(1+x2+x4)(1+x3+x6)
(Watch out: Tray A requires at least 1 notebook, so its bracket starts at x, not 1. Including 1 by mistake adds the invalid choice 1⋅1⋅x6 and gives the wrong answer 4.)
Because each bracket has only three terms, we match one term from each bracket whose exponents add up to 6:
x2⋅x4⋅1+x⋅x2⋅x3+x3⋅1⋅x3=3x6
Number of ways: 3
2
Find the closed-form generating function G(x) for the sequence 0,0,0,4,12,36,108,…, where a0=a1=a2=0 and an=4⋅3n−3 for all n≥3.
Watch out: Because the indexing starts at a0, the three leading zeros are a0,a1,a2 and the first nonzero term 4 sits on x3, not x4; starting at x4 gives the wrong answer 1−3x4x4.
Write out the first few nonzero terms attached to their powers of x: what common factor can you pull out of the front so the remaining series starts with 1?
Writing the sequence as a power series gives:
G(x)=4x3+12x4+36x5+108x6+…
(Watch out: Because the indexing starts at a0, the three leading zeros are a0,a1,a2 and the first nonzero term 4 sits on x3, not x4; starting at x4 gives the wrong answer 1−3x4x4.)
Factoring 4x3 out of every term leaves a geometric series with ratio r=3:
G(x)=4x3(1+3x+9x2+27x3+…)=1−3x4x3
3
Find the formula for an, the coefficient of xn, in the generating function G(x)=1+5x6+(1−2x)41.
Watch out: Missing the negative sign on r=−5 in 1+5x and forgetting to attach rn=2n to the second term gives the wrong answer 6⋅5n+C(n+3,3).
Unpack the two fractions separately: how do you rewrite +5x in the first denominator to match 1−rx, and what are k and r in the second denominator?
Rewrite the first denominator to show the negative ratio r=−5, and unpack the second fraction using Stars and Bars with k=4(k−1=3) and ratio r=2:
G(x)=1−(−5)x6+(1−2x)41
(Watch out: Missing the negative sign on r=−5 in 1+5x and forgetting to attach rn=2n to the second term gives the wrong answer 6⋅5n+C(n+3,3).)
Reading off the coefficient of xn from each part and adding them gives:
an=6(−5)n+C(n+3,3)2n
4
You want to make a box of 20 pastries from five types: croissants must be a multiple of 4, muffins at most 3, scones an even number, strudels at most 1, and tarts at least 2. Use generating functions to find the number of ways to make the box of 20 pastries.
Watch out: Plugging n=20 directly into C(n+2,2) without subtracting the 2 required tarts from the exponent gives the wrong answer C(22,2)=231.
Compress all five brackets into fractions before multiplying. Which numerators and denominators cancel out?
Compress each pastry rule into its closed form and multiply:
(Watch out: "At most 3" muffins stops at x3, so its numerator is 1−x4, one step past the cutoff, which cancels 1−x41.)
To find the coefficient of x20 in x2⋅(1−x)31, we pair the x2 in front with the x18 term of (1−x)31 using k=3:(Watch out: Plugging n=20 directly into C(n+2,2) without subtracting the 2 required tarts from the exponent gives the wrong answer C(22,2)=231.)
a20=C(18+3−1,3−1)=C(20,2)=190
5
You have 14 identical coins to distribute among 4 pouches so that every pouch gets at least 1 coin and at most 4 coins. Use generating functions to find the number of ways to distribute the coins.
Watch out: Because 12≤14, two pouches can go over the 4-coin limit at the same time, so +6x12 must be included; dropping +6x12 after the first subtraction gives −50, and forgetting to multiply x4 into (1−x4)4 before matching powers to x14 gives C(17,3)−4C(13,3)+6C(9,3)−4C(5,3)=0.
After factoring out x from each of the 4 pouches and compressing the remaining bracket, which powers in the expanded numerator are small enough to help form x14?
Factor x out of each of the 4 brackets, compress 1+x+x2+x3=1−x1−x4, and unpack the numerator and denominator (k=4, so k−1=3):
Pair each term xd in the first bracket with d≤14 with the x14−d term in the second bracket (ignoring −4x16 and x20): (Watch out: Because 12≤14, two pouches can go over the 4-coin limit at the same time, so +6x12 must be included; dropping +6x12 after the first subtraction gives −50, and forgetting to multiply x4 into (1−x4)4 before matching powers to x14 gives C(17,3)−4C(13,3)+6C(9,3)−4C(5,3)=0.)