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 xx

Before introducing generating functions, let's look at how ordinary bracket expansion already pairs up choices from different boxes.

Expanding Brackets to Count Combinations
Apple\text{Apple}
Banana\text{Banana}
Cherry\text{Cherry}
ways to take 2 fruits= ?\text{ways to take }\htmlClass{lc-pink}{2}\text{ fruits} = \,?

When we replace each option in a box with xamount takenx^{\itemcount{\text{amount taken}}}, multiplying one choice from each of the three fruit boxes adds their exponents (xa⋅xb=xa+bx^{\itemcount{a}} \cdot x^{\itemcount{b}} = x^{\itemcount{a+b}}), and combining like terms counts how many combinations produce the same total:

(x0+x1)(x0+x1)(x0+x1)=(1+x)3=1+3x+3⏟numberof ways x ⁣ ⁣2⏞totalfruits+x3\displaystyle \begin{aligned} &(x^{\itemcount{0}} + x^{\itemcount{1}})(x^{\itemcount{0}} + x^{\itemcount{1}})(x^{\itemcount{0}} + x^{\itemcount{1}}) \\ &\quad= (1 + x)^3 \\ &\quad= 1 + 3x + \underbrace{\waycount{3}}_{\substack{\text{number} \\ \text{of ways}}}\, x^{\!\!\overbrace{\itemcount{2}}^{\substack{\text{total} \\ \text{fruits}}}} + x^3 \end{aligned}

So there are 3\waycount{3} ways to walk away with 2\itemcount{2} fruits, even though xx 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\itemcount{3} fruits.

Encoding Rules as Generating Functions
at most 2 apples\text{at most }\htmlClass{lc-pink}{2}\text{ apples}
⟶  (    ?    )\longrightarrow\;(\;\;?\;\;)
at least 1, at most 3\text{at least }\htmlClass{lc-pink}{1}\text{, at most }\htmlClass{lc-pink}{3}
⟶  (    ?    )\longrightarrow\;(\;\;?\;\;)
bunches of 2, up to 4\text{bunches of }\htmlClass{lc-pink}{2}\text{, up to }\htmlClass{lc-pink}{4}
⟶  (    ?    )\longrightarrow\;(\;\;?\;\;)
Text solution
Encode each box's allowed counts as exponents of xxWe list the valid amounts from Box A (0,1,\itemcount{0}, \itemcount{1}, or 2\itemcount{2} apples) and Box B (0,2,\itemcount{0}, \itemcount{2}, or 4\itemcount{4} bananas) as powers of xx and multiply the two brackets:
G(x)=(1+x+x2)⏟0, 1, or 2 apples (1+x2+x4)⏟0, 2, or 4 bananas\displaystyle \begin{aligned} G(x) &= \underbrace{(1 + x + x^2)}_{\text{0, 1, or 2 apples}}\,\underbrace{(1 + x^2 + x^4)}_{\text{0, 2, or 4 bananas}} \end{aligned}
Pair terms whose exponents add up to 3\itemcount{3}Instead of expanding the entire product, we only match terms from the first bracket with terms from the second that multiply to x3x^{\itemcount{3}}:
x1⋅x2\displaystyle x^{\itemcount{1}} \cdot x^{\itemcount{2}} =1x3\displaystyle = \waycount{1}x^{\itemcount{3}}

So there is 1\waycount{1} way to make a basket of 3\itemcount{3} fruits (1\itemcount{1} apple and 2\itemcount{2} bananas).

Expanding the full product gives the count for every possible basket size n\itemcount{n} at once, which we call the generating function G(x)G(x) for the sequence a0,a1,a2,…\waycount{a_0}, \waycount{a_1}, \waycount{a_2}, \dots:

G(x)=a0+a1x+a2x2+⋯+an⏟ways to geta total of nxn+…\displaystyle \begin{aligned} G(x) &= \waycount{a_0} + \waycount{a_1} x + \waycount{a_2} x^2 \\ &\quad + \dots + \underbrace{\waycount{a_n}}_{\substack{\text{ways to get} \\ \text{a total of } \itemcount{n}}} x^{\itemcount{n}} + \dots \end{aligned}

In this two-box product, every allowed power xax^{\itemcount{a}} in the first bracket pairs with every allowed power xbx^{\itemcount{b}} in the second, and all products xa+bx^{\itemcount{a+b}} whose exponents add up to n\itemcount{n} combine into the xnx^{\itemcount{n}} 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\itemcount{n} is large, expanding brackets term by term is too slow. We want to compress a long or infinite series of powers of xx into a single fraction, called a closed form.

Compressing Series into Closed Forms
⋯\htmlClass{lc-mute}{\cdots}
S=\htmlClass{lc-blue}{S} =
1\htmlClass{lc-blue}{1}
++
x\htmlClass{lc-blue}{x}
++
x2\htmlClass{lc-blue}{x^2}
++
x3\htmlClass{lc-blue}{x^3}
++
x4\htmlClass{lc-blue}{x^4}
++
⋯\cdots
apples\htmlClass{lc-pink}{\text{apples}}
0\htmlClass{lc-pink}{0}
1\htmlClass{lc-pink}{1}
2\htmlClass{lc-pink}{2}
3\htmlClass{lc-pink}{3}
4\htmlClass{lc-pink}{4}
ways\htmlClass{lc-green}{\text{ways}}
1\htmlClass{lc-green}{1}
1\htmlClass{lc-green}{1}
1\htmlClass{lc-green}{1}
1\htmlClass{lc-green}{1}
1\htmlClass{lc-green}{1}
⋯\htmlClass{lc-green}{\cdots}
S in closed form= ?\htmlClass{lc-blue}{S}\text{ in closed form} = \,?

Multiplying the infinite series S=1+x+x2+x3+…\plainbox{S} = \plainbox{1 + x + x^2 + x^3 + \dots} by x\shift{x} shifts every power one step to the right, so subtracting the two lines cancels every term after the leading 11:

S−xS=(1+x+x2+x3+…)−(x+x2+x3+…)S(1−x)=1S=11−x\displaystyle \begin{aligned} \plainbox{S} - \shift{x}\plainbox{S} &= (1 + \cancelled{x + x^2 + x^3 + \dots}) \\ &\quad - (\cancelled{x + x^2 + x^3 + \dots}) \\ \plainbox{S}(1 - x) &= 1 \\ \plainbox{S} &= \plainbox{\frac{1}{1 - x}} \end{aligned}

When a bracket stops at a cutoff xmx^{\itemcount{m}}, shifting and subtracting in the same way leaves a single uncancelled −xm+1\leftover{-x^{m+1}} one step past the cutoff:

(1+x+x2+⋯+xm)(1−x)=1−xm+11+x+x2+⋯+xm=1−xm+11−x\displaystyle \begin{aligned} &(1 + x + x^2 + \dots + x^{\itemcount{m}})(1 - x) \\ &\qquad = 1 \leftover{- x^{m+1}} \\[4pt] &1 + x + x^2 + \dots + x^{\itemcount{m}} \\ &\qquad = \frac{1 \leftover{- x^{m+1}}}{1 - x} \end{aligned}

Whether the series stops at xmx^{\itemcount{m}} or continues forever, multiplying by 1−x1 - x pairs each middle term +xk+x^k with −xk-x^k from the term before it, leaving only 11 at the front and −xm+1\leftover{-x^{m+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 xx in 11−x\plainbox{\frac{1}{1 - x}} with xm\swap{x^m} (when exponents jump by mm) or with rx\swap{rx} (when each coefficient is r\swap{r} times the one before) gives the multiples-of-mm and ratio-r\swap{r} forms in this list of our core closed forms:

1+x+x2+x3+…=11−x(any amount: an=1)1+xm+x2m+x3m+…=11−xm(multiples of m)1+rx+r2x2+r3x3+…=11−rx(geometric ratio r: an=rn)1+x+x2+⋯+xm=1−xm+11−x(at most m)C(k,0)+C(k,1)x+⋯+C(k,k)xk=(1+x)k(Binomial Theorem)\displaystyle \begin{aligned} &\plainbox{1 + x + x^2 + x^3 + \dots} \\ &\quad = \plainbox{\frac{1}{1 - x}} \\ &\quad \text{(any amount: } \waycount{a_n} = 1\text{)} \\[10pt] &1 + x^m + x^{2m} + x^{3m} + \dots \\ &\quad = \frac{1}{1 - \swap{x^m}} \\ &\quad \text{(multiples of } m\text{)} \\[10pt] &1 + \swap{r}x + \swap{r}^2x^2 + \swap{r}^3x^3 + \dots \\ &\quad = \frac{1}{1 - \swap{rx}} \\ &\quad \text{(geometric ratio } \swap{r}\text{: } \waycount{a_n} = \swap{r}^n\text{)} \\[10pt] &1 + x + x^2 + \dots + x^{\itemcount{m}} \\ &\quad = \frac{1 \leftover{- x^{m+1}}}{1 - x} \\ &\quad \text{(at most } \itemcount{m}\text{)} \\[10pt] &\waycount{C(k,0)} + \waycount{C(k,1)}x + \dots + \waycount{C(k,k)}x^k \\ &\quad = (1 + x)^k \\ &\quad \text{(Binomial Theorem)} \end{aligned}
4

Translating Between Sequences and Closed Forms

To solve counting problems with algebra, we need to translate in both directions: compressing a sequence an\waycount{a_n} into a closed-form fraction G(x)G(x), and unpacking a fraction G(x)G(x) back into a formula for the coefficient an\waycount{a_n}.

Translating Between Sequences and Closed Forms
sequence  ⟶  closed form\text{sequence}\;\htmlClass{lc-violet}{\longrightarrow}\;\text{closed form}
1,3,9,27,81,…G(x)= ?\htmlClass{lc-green}{1, 3, 9, 27, 81, \dots}\qquad G(x) = \,?
closed form  ⟶  sequence\text{closed form}\;\htmlClass{lc-violet}{\longrightarrow}\;\text{sequence}
G(x)=11−4xan= ?G(x) = \dfrac{1}{1 - 4x}\qquad \htmlClass{lc-green}{a_n} = \,?
Text solution
Factor out the first nonzero power to compress a sequence with leading zerosFor 0,0,1,1,1,…\waycount{0, 0, 1, 1, 1}, \dots, the series starts at x2x^2, so we pull x2x^2 out of the front to leave 11−x\plainbox{\frac{1}{1 - x}}:
G(x)=0+0x+1x2+1x3+1x4+…=x2(1+x+x2+…)=x21−x\displaystyle \begin{aligned} G(x) &= \waycount{0} + \waycount{0}x + \waycount{1}x^2 \\ &\quad + \waycount{1}x^3 + \waycount{1}x^4 + \dots \\ &= x^2(\plainbox{1 + x + x^2 + \dots}) \\ &= \frac{x^2}{\plainbox{1 - x}} \end{aligned}
Rewrite +rx+ \swap{rx} as −(−rx)- (\swap{-rx}) to unpack a sum of fractionsTo find an\waycount{a_n} in G(x)=11−3x+41+xG(x) = \frac{1}{1 - \swap{3x}} + \frac{4}{1 + \swap{x}}, we identify the ratio r\swap{r} in each denominator and add their coefficients of xnx^{\itemcount{n}}:
G(x)=11−3x+41−(−1)xan=3n+4(−1)n\displaystyle \begin{aligned} G(x) &= \frac{1}{1 - \swap{3x}} + \frac{4}{1 - \swap{(-1)x}} \\ \waycount{a_n} &= \swap{3}^n + 4(\swap{-1})^n \end{aligned}
5

Counting by Canceling Closed Forms

Now we want to find the number of ways to make a bag of 50\itemcount{50} candies from four types (an even number of Red, at most 1\itemcount{1} Blue, a multiple of 3\itemcount{3} Green, and at most 2\itemcount{2} Yellow) by compressing each rule into a fraction before multiplying.

Counting by Canceling Closed Forms
a bag of 50 candies\text{a bag of }\htmlClass{lc-pink}{50}\text{ candies}
Red\text{Red}
even: 0,2,4,…\text{even: }\htmlClass{lc-pink}{0, 2, 4, \dots}
Blue\text{Blue}
at most 1: 0,1\text{at most }\htmlClass{lc-pink}{1}\text{: }\htmlClass{lc-pink}{0, 1}
Green\text{Green}
multiple of 3: 0,3,6,…\text{multiple of }\htmlClass{lc-pink}{3}\text{: }\htmlClass{lc-pink}{0, 3, 6, \dots}
Yellow\text{Yellow}
at most 2: 0,1,2\text{at most }\htmlClass{lc-pink}{2}\text{: }\htmlClass{lc-pink}{0, 1, 2}
ways to fill the bag= ?\text{ways to fill the bag} = \,?
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)=(1+x2+x4+… )⏟Red: even⋅(1+x)⏟Blue:at most 1⋅(1+x3+x6+… )⏟Green: multiple of 3⋅(1+x+x2)⏟Yellow:at most 2=11−x2⋅1−x21−x⋅11−x3⋅1−x31−x\displaystyle \begin{aligned} G(x) &= \underbrace{(1 + x^2 + x^4 + \dots)}_{\text{Red: even}} \\ &\quad \cdot \underbrace{(1 + x)}_{\substack{\text{Blue:} \\ \text{at most } \itemcount{1}}} \\ &\quad \cdot \underbrace{(1 + x^3 + x^6 + \dots)}_{\text{Green: multiple of } \itemcount{3}} \\ &\quad \cdot \underbrace{(1 + x + x^2)}_{\substack{\text{Yellow:} \\ \text{at most } \itemcount{2}}} \\ &= \frac{1}{\cancelled{1 - x^2}} \cdot \frac{\cancelled{1 - x^2}}{1 - x} \\ &\quad \cdot \frac{1}{\cancelled{1 - x^3}} \cdot \frac{\cancelled{1 - x^3}}{1 - x} \end{aligned}
Cancel matching factors across numerators and denominatorsMultiplying the fractions cancels both (1−x2)\cancelled{(1 - x^2)} and (1−x3)\cancelled{(1 - x^3)}:
G(x)=11−x⋅11−x=1(1−x)2\displaystyle \begin{aligned} G(x) &= \plainbox{\frac{1}{1 - x}} \cdot \plainbox{\frac{1}{1 - x}} \\ &= \plainbox{\frac{1}{(1 - x)^2}} \end{aligned}
Unpack the simplified closed form at n=50\itemcount{n} = \itemcount{50}Because 1(1−x)2\plainbox{\frac{1}{(1 - x)^2}} is two unrestricted boxes (1+x+x2+…)(\plainbox{1 + x + x^2 + \dots}) (1+x+x2+…)(\plainbox{1 + x + x^2 + \dots}), there are n+1\waycount{n + 1} ways to split n\itemcount{n} items between them:
a50\displaystyle \waycount{a_{\itemcount{50}}} =50+1\displaystyle = \itemcount{50} + 1 =51\displaystyle = \waycount{51}

So there are 51\waycount{51} ways to make a bag of 50\itemcount{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\itemcount{10} identical cookies among 33 kids so that every kid gets at least 1\itemcount{1} cookie and at most 4\itemcount{4} cookies.

When we compress kk brackets, the denominator becomes (1−x)k\plainbox{(1 - x)^k}. Because 11−x\plainbox{\frac{1}{1 - x}} is a single unrestricted box 1+x+x2+…\plainbox{1 + x + x^2 + \dots}, the power 1(1−x)k\plainbox{\frac{1}{(1 - x)^k}} is kk unrestricted boxes multiplied together. Picking n\itemcount{n} items in total from kk unrestricted boxes is the Stars and Bars count of placing nn stars into kk boxes separated by k−1k - 1 bars, which is C(n+k−1,k−1)\waycount{C(n + k - 1, k - 1)}.

(1 ⁣+ ⁣x ⁣+ ⁣x2 ⁣+ ⁣⋯ )\htmlClass{lc-blue}{(1\!+\!x\!+\!x^2\!+\!\cdots)}
(1 ⁣+ ⁣x ⁣+ ⁣x2 ⁣+ ⁣⋯ )\htmlClass{lc-blue}{(1\!+\!x\!+\!x^2\!+\!\cdots)}
(1 ⁣+ ⁣x ⁣+ ⁣x2 ⁣+ ⁣⋯ )\htmlClass{lc-blue}{(1\!+\!x\!+\!x^2\!+\!\cdots)}
x2x^{\htmlClass{lc-pink}{2}}
⋅\cdot
x3x^{\htmlClass{lc-pink}{3}}
⋅\cdot
x2x^{\htmlClass{lc-pink}{2}}
n stars,k−1=2 barsn\text{ stars},\quad k - 1 = 2\text{ bars}
C(n+k−1, k−1) xn\htmlClass{lc-green}{C(n + k - 1,\, k - 1)}\,x^{\htmlClass{lc-pink}{n}}

Unpacking 1(1−x)k\plainbox{\frac{1}{(1 - x)^k}} (or 1(1−rx)k\frac{1}{(1 - \swap{rx})^k} when xx is replaced by rx\swap{rx}) gives the coefficient of xnx^{\itemcount{n}} for any kk boxes:

1(1−x)k=∑n=0∞C(n+k−1,k−1) xn1(1−rx)k=∑n=0∞C(n+k−1,k−1) rnxn\displaystyle \begin{aligned} &\plainbox{\frac{1}{(1 - x)^k}} \\ &\quad = \sum_{n=0}^{\infty} \waycount{C(n + k - 1, k - 1)}\,x^n \\[16pt] &\frac{1}{(1 - \swap{rx})^k} \\ &\quad = \sum_{n=0}^{\infty} \waycount{C(n + k - 1, k - 1)}\,\swap{r}^n x^n \end{aligned}

Let's use this unpacking rule to find the coefficient of x10x^{\itemcount{10}} in the 10\itemcount{10}-cookie problem.

Extracting Coefficients When Terms Do Not Cancel
10 identical cookies\htmlClass{lc-pink}{10}\text{ identical cookies}
each kid: at least 1, at most 4\text{each kid: at least }\htmlClass{lc-pink}{1}\text{, at most }\htmlClass{lc-pink}{4}
ways to hand them out= ?\text{ways to hand them out} = \,?
Text solution
Encode the rule and factor out the minimum requirementEach of the 33 kids gets 1,2,3,\itemcount{1}, \itemcount{2}, \itemcount{3}, or 4\itemcount{4} cookies, and factoring xx out of each bracket gives 1\itemcount{1} cookie to each kid up front:
G(x)=(x+x2+x3+x4)3=x3(1+x+x2+x3)3\displaystyle \begin{aligned} G(x) &= (x + x^2 + x^3 + x^4)^3 \\ &= x^3(1 + x + x^2 + x^3)^3 \end{aligned}
Compress the bracket and pull the numerator and denominator apartThe bracket stops at x3x^{\itemcount{3}}, so it compresses into 1−x41−x\frac{1 \leftover{- x^4}}{1 - x}:
G(x)=x3(1−x41−x)3=x3(1−x4)3⋅1(1−x)3\displaystyle \begin{aligned} G(x) &= x^3 \left(\frac{1 \leftover{- x^4}}{1 - x}\right)^3 \\ &= x^3(1 \leftover{- x^4})^3 \cdot \plainbox{\frac{1}{(1 - x)^3}} \end{aligned}
Unpack the top and bottom pieces separatelyWe expand (1−x4)3(1 \leftover{- x^4})^3 with the Binomial Theorem and unpack 1(1−x)3\plainbox{\frac{1}{(1 - x)^3}} using Stars and Bars with k=3k = 3 (k−1=2k - 1 = 2):
G(x)=x3(1−3x4+3x8−x12)⋅∑n=0∞C(n+2,2) xn=(x3−3x7+3x11−x15)⋅(1+⋯+C(5,2)x3+⋯+C(9,2)x7+… )\displaystyle \begin{aligned} G(x) &= x^3(1 \overlimit{- 3x^4} + 3x^8 \overlimit{- x^{12}}) \\ &\quad \cdot \sum_{n=0}^{\infty} \waycount{C(n + 2, 2)}\,x^n \\ &= (x^3 \overlimit{- 3x^7} + 3x^{11} \overlimit{- x^{15}}) \\ &\quad \cdot \Bigl(1 + \dots + \waycount{C(5, 2)}x^3 \\ &\quad + \dots + \waycount{C(9, 2)}x^7 + \dots\Bigr) \end{aligned}
Pair terms whose exponents add up to 10\itemcount{10}We match each term xdx^d in the first bracket (d≤10d \le \itemcount{10}) with x10−dx^{\itemcount{10 - d}} in the second bracket, and drop 3x11\pasttarget{3x^{11}} and −x15\pasttarget{-x^{15}} because their exponents already exceed 10\itemcount{10}:
a10=1⋅C(7+2,2)−3⋅C(3+2,2)=C(9,2)−3 C(5,2)=36−30=6\displaystyle \begin{aligned} \waycount{a_{\itemcount{10}}} &= 1 \cdot \waycount{C(\itemcount{7} + 2, 2)} \\ &\quad \overlimit{- 3 \cdot C(\itemcount{3} + 2, 2)} \\ &= \waycount{C(9, 2)} \overlimit{- 3\,C(5, 2)} \\ &= \waycount{36} \overlimit{- 30} \\ &= \waycount{6} \end{aligned}

So there are 6\waycount{6} ways to distribute the 10\itemcount{10} cookies among the 33 kids.

You can also read the count before multiplying x3x^3 into (1−3x4+3x8−x12)(1 \overlimit{- 3x^4} + 3x^8 \overlimit{- x^{12}}): once x3x^3 accounts for the 3\itemcount{3} required cookies, you just pair the terms of (1−3x4+3x8−x12)(1 \overlimit{- 3x^4} + 3x^8 \overlimit{- x^{12}}) with 1(1−x)3\plainbox{\frac{1}{(1 - x)^3}} to make the remaining x7x^{\itemcount{7}}, which gives the same 1⋅C(7+2,2)−3⋅C(3+2,2)=61 \cdot \waycount{C(\itemcount{7} + 2, 2)} \overlimit{- 3 \cdot C(\itemcount{3} + 2, 2)} = \waycount{6}.

As the target power xnx^{\itemcount{n}} increases, each term of x3−3x7+3x11−x15x^3 \overlimit{- 3x^7} + 3x^{11} \overlimit{- x^{15}} starts contributing only when n\itemcount{n} is large enough for that many kids to go over the 4\itemcount{4}-cookie limit at once, and for n>12\itemcount{n} > 12 the included terms cancel to zero, because 33 kids can hold at most 1212 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 xx: a minimum of a\itemcount{a} items starts at xax^{\itemcount{a}} (which you can factor out of the front), a maximum of m\itemcount{m} items stops at xmx^{\itemcount{m}}, and packs of size m\itemcount{m} use only multiples of m\itemcount{m} (1+xm+x2m+…1 + x^{\itemcount{m}} + x^{\itemcount{2m}} + \dots).

If you only have two or three short brackets and a small target total, like finding the coefficient of x3x^{\itemcount{3}} in (1+x+x2)(1+x2+x4)(1 + x + x^2)(1 + x^2 + x^4), do not compress the brackets into fractions; just pick the pairs of terms from each bracket whose exponents add up to 3\itemcount{3} directly.

When the brackets are infinite or the target power n\itemcount{n} is large, compress each bracket before multiplying:

Key concept

Infinite steps of size mm: 1+xm+x2m+⋯=11−xm1 + x^m + x^{2m} + \dots = \frac{1}{1 - \swap{x^m}}

Key concept

Finite cutoff at xmx^{\itemcount{m}}: 1+x+x2+⋯+xm=1−xm+11−x1 + x + x^2 + \dots + x^{\itemcount{m}} = \frac{1 \leftover{- x^{m+1}}}{1 - x} (the top exponent is m+1\leftover{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 xnx^{\itemcount{n}} from what remains:

Key concept

A single geometric denominator 11−rx\frac{1}{1 - \swap{rx}} unpacks to an=rn\waycount{a_n} = \swap{r}^n (so 71+2x=71−(−2)x\frac{7}{1 + \swap{2x}} = \frac{7}{1 - \swap{(-2)x}} gives an=7(−2)n\waycount{a_n} = 7(\swap{-2})^n).

Key concept

A repeated denominator 1(1−rx)k\frac{1}{(1 - \swap{rx})^k} unpacks via Stars and Bars to an=C(n+k−1,k−1) rn\waycount{a_n} = \waycount{C(n + k - 1, k - 1)}\,\swap{r}^n (so 1(1−x)3\plainbox{\frac{1}{(1 - x)^3}} gives an=C(n+2,2)\waycount{a_n} = \waycount{C(n + 2, 2)}).

Key concept

A remaining numerator like (1−x4)3(1 \leftover{- x^4})^3 alongside 1(1−x)3\plainbox{\frac{1}{(1 - x)^3}} expands by the Binomial Theorem into 1−3x4+3x8−x121 \overlimit{- 3x^4} + 3x^8 \overlimit{- x^{12}}; pair each term xdx^d (d≤nd \le \itemcount{n}) with the xn−dx^{\itemcount{n - d}} term from the bottom and ignore powers larger than n\itemcount{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 nn items, and find the number of ways to make a bag of 6 items total.

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 66?

Encoding the allowed item counts for each tray gives:
G(x)\displaystyle G(x) =(x+x2+x3)(1+x2+x4)(1+x3+x6)\displaystyle = (x + x^2 + x^3)(1 + x^2 + x^4)(1 + x^3 + x^6)
(Watch out: Tray A requires at least 1\itemcount{1} notebook, so its bracket starts at xx, not 11. Including 11 by mistake adds the invalid choice 1⋅1⋅x6\wrongcount{1 \cdot 1 \cdot x^6} and gives the wrong answer 4\wrongcount{4}.)
Because each bracket has only three terms, we match one term from each bracket whose exponents add up to 6\itemcount{6}:
x2⋅x4⋅1+x⋅x2⋅x3+x3⋅1⋅x3=3x6\displaystyle \begin{aligned} x^2 \cdot x^4 \cdot 1 + x \cdot x^2 \cdot x^3 + x^3 \cdot 1 \cdot x^3 &= \waycount{3}x^{\itemcount{6}} \end{aligned}
Number of ways: 3\waycount{3}
2

Find the closed-form generating function G(x)G(x) for the sequence 00, 00, 00, 44, 1212, 3636, 108108, …\dots, where a0=a1=a2=0a_0 = a_1 = a_2 = 0 and an=4⋅3n−3a_n = 4 \cdot 3^{n-3} for all n≥3n \ge 3.

Write out the first few nonzero terms attached to their powers of xx: what common factor can you pull out of the front so the remaining series starts with 11?

Writing the sequence as a power series gives:
G(x)\displaystyle G(x) =4x3+12x4+36x5+108x6+…\displaystyle = \waycount{4}x^3 + \waycount{12}x^4 + \waycount{36}x^5 + \waycount{108}x^6 + \dots
(Watch out: Because the indexing starts at a0\waycount{a_0}, the three leading zeros are a0,a1,a2\waycount{a_0}, \waycount{a_1}, \waycount{a_2} and the first nonzero term 4\waycount{4} sits on x3x^{\itemcount{3}}, not x4x^4; starting at x4x^4 gives the wrong answer 4x41−3x\wrongcount{\frac{4x^4}{1 - 3x}}.)
Factoring 4x34x^3 out of every term leaves a geometric series with ratio r=3\swap{r} = \swap{3}:
G(x)=4x3(1+3x+9x2+27x3+… )=4x31−3x\displaystyle \begin{aligned} G(x) &= 4x^3(1 + 3x + 9x^2 + 27x^3 + \dots) \\ &= \frac{4x^3}{1 - \swap{3x}} \end{aligned}
3

Find the formula for ana_n, the coefficient of xnx^n, in the generating function G(x)=61+5x+1(1−2x)4G(x) = \frac{6}{1 + 5x} + \frac{1}{(1 - 2x)^4}.

Unpack the two fractions separately: how do you rewrite +5x+ 5x in the first denominator to match 1−rx1 - rx, and what are kk and rr in the second denominator?

Rewrite the first denominator to show the negative ratio r=−5\swap{r} = \swap{-5}, and unpack the second fraction using Stars and Bars with k=4k = 4 (k−1=3k - 1 = 3) and ratio r=2\swap{r} = \swap{2}:
G(x)=61−(−5)x+1(1−2x)4\displaystyle \begin{aligned} G(x) &= \frac{6}{1 - \swap{(-5)x}} + \frac{1}{(1 - \swap{2x})^4} \end{aligned}
(Watch out: Missing the negative sign on r=−5\swap{r} = \swap{-5} in 1+5x1 + \swap{5x} and forgetting to attach rn=2n\swap{r}^n = \swap{2}^n to the second term gives the wrong answer 6⋅5n+C(n+3,3)\wrongcount{6 \cdot 5^n + C(n + 3, 3)}.)
Reading off the coefficient of xnx^{\itemcount{n}} from each part and adding them gives:
an\displaystyle \waycount{a_n} =6(−5)n+C(n+3,3)2n\displaystyle = 6(\swap{-5})^n + \waycount{C(n + 3, 3)}\swap{2}^n
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.

Compress all five brackets into fractions before multiplying. Which numerators and denominators cancel out?

Compress each pastry rule into its closed form and multiply:
G(x)=11−x4⋅1−x41−x⋅11−x2⋅1−x21−x⋅x21−x=x2(1−x)3\displaystyle \begin{aligned} G(x) &= \frac{1}{\cancelled{1 - x^4}} \cdot \frac{\cancelled{1 - x^4}}{1 - x} \cdot \frac{1}{\cancelled{1 - x^2}} \cdot \frac{\cancelled{1 - x^2}}{1 - x} \cdot \frac{x^2}{1 - x} \\ &= \frac{x^2}{\plainbox{(1 - x)^3}} \end{aligned}
(Watch out: "At most 3\itemcount{3}" muffins stops at x3x^{\itemcount{3}}, so its numerator is 1−x4\cancelled{1 - x^4}, one step past the cutoff, which cancels 11−x4\frac{1}{\cancelled{1 - x^4}}.)
To find the coefficient of x20x^{\itemcount{20}} in x2⋅1(1−x)3x^2 \cdot \plainbox{\frac{1}{(1 - x)^3}}, we pair the x2x^2 in front with the x18x^{\itemcount{18}} term of 1(1−x)3\plainbox{\frac{1}{(1 - x)^3}} using k=3k = 3: (Watch out: Plugging n=20n = 20 directly into C(n+2,2)C(n + 2, 2) without subtracting the 22 required tarts from the exponent gives the wrong answer C(22,2)=231\wrongcount{C(22, 2) = 231}.)
a20=C(18+3−1,3−1)=C(20,2)=190\displaystyle \begin{aligned} \waycount{a_{\itemcount{20}}} &= C(\itemcount{18} + 3 - 1, 3 - 1) \\ &= C(20, 2) \\ &= \waycount{190} \end{aligned}
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.

After factoring out xx from each of the 44 pouches and compressing the remaining bracket, which powers in the expanded numerator are small enough to help form x14x^{14}?

Factor xx out of each of the 44 brackets, compress 1+x+x2+x3=1−x41−x1 + x + x^2 + x^3 = \frac{1 \leftover{- x^4}}{1 - x}, and unpack the numerator and denominator (k=4k = 4, so k−1=3k - 1 = 3):
G(x)=(x+x2+x3+x4)4=x4(1−x4)4⋅1(1−x)4=(x4−4x8+6x12−4x16+x20)⋅∑n=0∞C(n+3,3) xn\displaystyle \begin{aligned} G(x) &= (x + x^2 + x^3 + x^4)^4 \\ &= x^4(1 \leftover{- x^4})^4 \cdot \plainbox{\frac{1}{(1 - x)^4}} \\ &= (x^4 \overlimit{- 4x^8} + 6x^{12} \overlimit{- 4x^{16}} + x^{20}) \cdot \sum_{n=0}^{\infty} \waycount{C(n + 3, 3)}\,x^n \end{aligned}
Pair each term xdx^d in the first bracket with d≤14d \le \itemcount{14} with the x14−dx^{\itemcount{14 - d}} term in the second bracket (ignoring −4x16\pasttarget{-4x^{16}} and x20\pasttarget{x^{20}}): (Watch out: Because 12≤1412 \le \itemcount{14}, two pouches can go over the 4\itemcount{4}-coin limit at the same time, so +6x12+6x^{12} must be included; dropping +6x12+6x^{12} after the first subtraction gives −50\wrongcount{-50}, and forgetting to multiply x4x^4 into (1−x4)4(1 \leftover{- x^4})^4 before matching powers to x14x^{\itemcount{14}} gives C(17,3)−4 C(13,3)+6 C(9,3)−4 C(5,3)C(17, 3) - 4\,C(13, 3) + 6\,C(9, 3) - 4\,C(5, 3) == 0\wrongcount{0}.)
a14=C(10+3,3)−4 C(6+3,3)+6 C(2+3,3)=C(13,3)−4 C(9,3)+6 C(5,3)=286−336+60=10\displaystyle \begin{aligned} \waycount{a_{\itemcount{14}}} &= \waycount{C(\itemcount{10} + 3, 3)} \overlimit{- 4\,C(\itemcount{6} + 3, 3)} + 6\,C(\itemcount{2} + 3, 3) \\ &= \waycount{C(13, 3)} \overlimit{- 4\,C(9, 3)} + 6\,C(5, 3) \\ &= \waycount{286} \overlimit{- 336} + 60 \\ &= \waycount{10} \end{aligned}