Skip to content

Ordinary Generating Functions

In Sections 07 and 08, we counted integer solutions to equations like \(x_1 + x_2 + \dots + x_r = n\) using binary strings, variable shifts, and the Principle of Inclusion–Exclusion. Each new restriction—such as upper bounds, parity conditions, or weighted coefficients—required a separate counting trick.

Generating functions turn all of these counting problems into one unified method: multiplying polynomials and power series. Before stating any formula, we build the idea slowly from basic algebra so it is completely clear why it works.


How Multiplying Brackets Works

Consider a simple algebraic product of two brackets:

\[(a_1 + b_1)(a_2 + b_2) = a_1 a_2 + a_1 b_2 + b_1 a_2 + b_1 b_2\]

Look closely at how every term on the right is formed. We do not mix terms from the same bracket. Instead, to produce one product term, we:

  • pick exactly one term from the first bracket (\(a_1\) or \(b_1\)),
  • pick exactly one term from the second bracket (\(a_2\) or \(b_2\)),
  • multiply those two chosen terms together.

Summing over every possible pair of choices gives the complete expansion.

Now try brackets of unequal sizes, such as \((a_1 + b_1 + c_1)(a_2 + b_2)\):

\[(a_1 + b_1 + c_1)(a_2 + b_2) = a_1 a_2 + a_1 b_2 + b_1 a_2 + b_1 b_2 + c_1 a_2 + c_1 b_2\]

There are \(3\) choices in the first bracket and \(2\) choices in the second bracket. By the Multiplication Principle (Section 01), expanding the product generates \(3 \times 2 = 6\) raw products—one for every possible pair of picks.

Expanding a Product of Two Brackets by Picking One Term From Each

The same rule holds for three or more brackets. For \((a_1 + b_1)(a_2 + b_2)(a_3 + b_3)\):

\[(a_1 + b_1)(a_2 + b_2)(a_3 + b_3) = a_1 a_2 a_3 + a_1 a_2 b_3 + a_1 b_2 a_3 + a_1 b_2 b_3 + b_1 a_2 a_3 + b_1 a_2 b_3 + b_1 b_2 a_3 + b_1 b_2 b_3\]

There are \(2 \times 2 \times 2 = 8\) raw products, corresponding to every way of choosing one term from each of the three brackets.

In general, when we expand a product of \(r\) brackets whose sizes are \(k_1, k_2, \dots, k_r\):

Core Rule of Expansion: Every raw term in the product is obtained by selecting exactly one term from each of the \(r\) brackets and multiplying them together. Before collecting like terms, there are \(k_1 k_2 \cdots k_r\) raw products—one for every possible tuple of choices.


Turning Choices into Exponents of One Symbol

Now make one small but decisive change. Instead of putting distinct letters inside the brackets, place powers of a single symbol \(x\) inside every bracket.

Consider the product:

\[(x^0 + x^1 + x^2)(x^0 + x^1 + x^2)\]

Expand it slowly by our bracket-picking rule, writing out all \(3 \times 3 = 9\) raw products before simplifying:

\[x^0 \cdot x^0 + x^0 \cdot x^1 + x^0 \cdot x^2 + x^1 \cdot x^0 + x^1 \cdot x^1 + x^1 \cdot x^2 + x^2 \cdot x^0 + x^2 \cdot x^1 + x^2 \cdot x^2\]

Now apply the law of exponents, \(x^a \cdot x^b = x^{a + b}\). Multiplying two chosen terms adds their exponents! Group the \(9\) raw products by their total exponent:

  • Exponent \(0\) (\(x^0\)): \(x^0 \cdot x^0\) \(\longrightarrow\) \(1\) way \(\longrightarrow\) solution \((0, 0)\) of \(x_1 + x_2 = 0\).
  • Exponent \(1\) (\(x^1\)): \(x^0 \cdot x^1 + x^1 \cdot x^0\) \(\longrightarrow\) \(2\) ways \(\longrightarrow\) solutions \((0, 1),\; (1, 0)\) of \(x_1 + x_2 = 1\).
  • Exponent \(2\) (\(x^2\)): \(x^0 \cdot x^2 + x^1 \cdot x^1 + x^2 \cdot x^0\) \(\longrightarrow\) \(3\) ways \(\longrightarrow\) solutions \((0, 2),\; (1, 1),\; (2, 0)\) of \(x_1 + x_2 = 2\).
  • Exponent \(3\) (\(x^3\)): \(x^1 \cdot x^2 + x^2 \cdot x^1\) \(\longrightarrow\) \(2\) ways \(\longrightarrow\) solutions \((1, 2),\; (2, 1)\) of \(x_1 + x_2 = 3\).
  • Exponent \(4\) (\(x^4\)): \(x^2 \cdot x^2\) \(\longrightarrow\) \(1\) way \(\longrightarrow\) solution \((2, 2)\) of \(x_1 + x_2 = 4\).

Putting the grouped terms together:

\[(1 + x + x^2)(1 + x + x^2) = 1 + 2x + 3x^2 + 2x^3 + x^4\]

Look at what algebra just did automatically:

  • Choosing \(x^{a_1}\) from the first bracket is identical to setting \(x_1 = a_1 \in \{0, 1, 2\}\).
  • Choosing \(x^{a_2}\) from the second bracket is identical to setting \(x_2 = a_2 \in \{0, 1, 2\}\).
  • Their product \(x^{a_1} \cdot x^{a_2} = x^{a_1 + a_2}\) equals \(x^n\) if and only if \(a_1 + a_2 = n\).
  • Every valid solution \((a_1, a_2)\) contributes exactly \(+1\) to the coefficient of \(x^n\).

Therefore, the coefficient of \(x^n\) in the expanded product equals the exact number of integer solutions to \(x_1 + x_2 = n\) with \(x_1, x_2 \in \{0, 1, 2\}\).

The exact same bijection works for \(r\) variables. For any \(r\) brackets representing the allowed values of \(x_1, x_2, \dots, x_r\), picking \(x^{a_1}\) from bracket \(1\), \(x^{a_2}\) from bracket \(2\), \(\dots\), and \(x^{a_r}\) from bracket \(r\) gives:

\[x^{a_1} \cdot x^{a_2} \cdots x^{a_r} = x^{a_1 + a_2 + \dots + a_r}\]

Why Multiplying Brackets Counts Solutions of x1 + x2 + x3 = 4

Coefficient Notation and the Role of the Placeholder

We write \([x^n]\,F(x)\) to mean "the coefficient of \(x^n\) in the expansion of \(F(x)\)." For instance:

\[[x^2](1 + x + x^2)^2 = 3, \qquad [x^3](1 + x + x^2)^2 = 2\]

What Is \(x\) Here?
In combinatorics, \(x\) is not a number we are trying to solve for. It is purely a formal bookkeeping placeholder whose exponents add whenever we multiply brackets. Because we never substitute numerical values for \(x\), we treat infinite sums of powers of \(x\) as formal power series—we do not need to worry about convergence tests such as \(|x| < 1\).


Solving Non-Negative Integer Equations: Natural Bound vs. Infinity

Now apply this method to the fundamental equation from Section 07:

\[x_1 + x_2 + \dots + x_r = n, \qquad x_i \ge 0 \quad (1 \le i \le r)\]

Here, no upper bound is stated for any variable \(x_i\). What bracket should we write for each \(x_i\)?

Step 1: The Natural Upper Bound

Even though no upper bound is given in the problem, every variable satisfies \(x_j \ge 0\). Therefore, no single variable can ever exceed the total sum \(n\):

\[0 \le x_i \le n \quad \text{for every } i = 1, 2, \dots, r\]

We call \(n\) the natural upper bound of the equation. Listing every value that \(x_i\) can actually take gives the exact finite polynomial \((1 + x + x^2 + \dots + x^n)\) for each bracket. Hence the number of non-negative integer solutions is:

\[[x^n]\,(1 + x + x^2 + \dots + x^n)^r\]

Step 2: Why Extending to Infinity Does Not Change the Answer

Now ask a careful question: what happens to \([x^n]\) if we replace the finite bracket \((1 + x + x^2 + \dots + x^n)\) with the infinite series \((1 + x + x^2 + \dots + x^n + x^{n+1} + x^{n+2} + \dots)\)?

Theorem (Safe Extension to Infinity): If every bracket in a product contains only non-negative powers of \(x\), then adding terms of degree strictly greater than \(n\) (that is, \(x^{n+1} + x^{n+2} + \dots\)) to any bracket does not change the coefficient of \(x^n\).

Proof: Suppose we form a raw product by picking one term \(x^{a_i}\) from each bracket, where every \(a_i \ge 0\). If we pick any newly added term \(x^k\) with \(k \ge n + 1\) from some bracket, then the sum of exponents across all \(r\) brackets satisfies:

\[a_1 + a_2 + \dots + a_r \ge k + 0 + \dots + 0 = k \ge n + 1 > n\]

Thus no raw product that uses a term from the infinite tail \(x^{n+1} + x^{n+2} + \dots\) can ever yield \(x^n\). The set of raw products that equal \(x^n\) is completely unchanged, so \([x^n]\) stays identical. \(\blacksquare\)

Natural Bound vs Infinite Tail and Active Upper Bound

Step 3: Why Extending to Infinity Makes Algebra Simpler

Using the geometric series formula \(1 + x + x^2 + \dots + x^n = \frac{1 - x^{n+1}}{1 - x}\) and its infinite counterpart \(1 + x + x^2 + \dots = \frac{1}{1 - x}\), compare the two expressions:

Finite form (stopping at \(x^n\)):

\[[x^n]\left(\frac{1 - x^{n+1}}{1 - x}\right)^r = [x^n]\,(1 - x^{n+1})^r (1 - x)^{-r}\]

Expanding \((1 - x^{n+1})^r = 1 - \binom{r}{1} x^{n+1} + \binom{r}{2} x^{2n+2} - \dots\) shows that every term except \(1\) already has degree \(> n\), so all of them vanish when we extract \([x^n]\)!

Infinite form (extending to \(\infty\)):

\[[x^n]\,(1 + x + x^2 + \dots)^r = [x^n]\left(\frac{1}{1 - x}\right)^r = [x^n]\,(1 - x)^{-r}\]

Extending an unrestricted bracket up to \(\infty\) throws away the useless \((1 - x^{n+1})^r\) factor upfront, leaving the clean expression \((1 - x)^{-r}\).

Essential Precaution (No Negative Powers):
The extension-to-infinity theorem requires every bracket to have only non-negative exponents. If another bracket contains a negative power such as \(x^{-2}\), then a tail term \(x^{n+2}\) multiplied by \(x^{-2}\) would produce \(x^n\) and corrupt the count! Whenever a variable has a negative lower bound, always shift it to a non-negative variable first.


The Master Expansion Formula

To compute \([x^m]\,(1 - x)^{-r}\) quickly and rigorously, we need its explicit series expansion.

Theorem (Negative-Index Binomial Series): For every positive integer \(r \ge 1\):

\[(1 - x)^{-r} = \frac{1}{(1 - x)^r} = \sum_{m=0}^{\infty} \binom{m + r - 1}{r - 1} x^m\]

Equivalently, for every integer \(m \ge 0\):

\[[x^m]\,(1 - x)^{-r} = \binom{m + r - 1}{r - 1}\]

We prove this in two independent ways—both use results we already established earlier in this chapter.

Proof 1 (Combinatorial Proof via Section 07): Rewrite \((1 - x)^{-r}\) as \((1 + x + x^2 + \dots)^r\). By our exponent-addition bijection, \([x^m]\,(1 + x + x^2 + \dots)^r\) equals the number of non-negative integer solutions to:

\[x_1 + x_2 + \dots + x_r = m, \qquad x_i \ge 0\]

By the Combinations with Unlimited Repetition Theorem (Section 07), this count is \(\binom{m + r - 1}{r - 1}\). Since this holds for every \(m \ge 0\), the expansion follows immediately. \(\blacksquare\)

Proof 2 (Algebraic Proof by Induction on \(r\)):

Base case (\(r = 1\)): By the geometric series identity:

\[(1 - x)^{-1} = 1 + x + x^2 + x^3 + \dots = \sum_{m=0}^{\infty} 1 \cdot x^m = \sum_{m=0}^{\infty} \binom{m + 1 - 1}{1 - 1} x^m\]

since \(\binom{m}{0} = 1\). The base case holds.

Inductive step: Assume the formula holds for \(r\). Write \((1 - x)^{-(r+1)} = (1 - x)^{-r} \cdot (1 - x)^{-1}\):

\[(1 - x)^{-(r+1)} = \left(\sum_{k=0}^{\infty} \binom{k + r - 1}{r - 1} x^k\right)\left(1 + x + x^2 + \dots\right)\]

To obtain \(x^m\) from this product, we pick \(x^k\) from the first series (for any \(0 \le k \le m\)) and \(x^{m - k}\) from the second series. Summing over all \(k = 0, 1, \dots, m\):

\[[x^m]\,(1 - x)^{-(r+1)} = \sum_{k=0}^{m} \binom{k + r - 1}{r - 1} = \binom{r - 1}{r - 1} + \binom{r}{r - 1} + \dots + \binom{m + r - 1}{r - 1}\]

By the Hockey-Stick Identity (Section 04), this sum collapses to \(\binom{m + r}{r}\), which matches the theorem for \(r + 1\). By mathematical induction, the formula holds for all \(r \ge 1\). \(\blacksquare\)

(Note: This also agrees with the generalized Binomial Theorem \((1 - x)^{-r} = \sum_{m=0}^{\infty} \binom{-r}{m}(-x)^m\), since \(\binom{-r}{m}(-1)^m = \frac{(-r)(-r-1)\cdots(-r-m+1)}{m!}(-1)^m = \frac{r(r+1)\cdots(r+m-1)}{m!} = \binom{m + r - 1}{m} = \binom{m + r - 1}{r - 1}\).)

Example 1: Use generating functions to find the number of non-negative integer solutions to \(x_1 + x_2 + x_3 + x_4 = 10\).

Solution: Each variable \(x_i \ge 0\) has no upper bound, so each contributes the factor \((1 + x + x^2 + \dots) = (1 - x)^{-1}\). The number of solutions is:

\[[x^{10}]\,(1 - x)^{-4} = \binom{10 + 4 - 1}{4 - 1} = \binom{13}{3} = \frac{13 \times 12 \times 11}{6} = 286\]

Encoding Restrictions into Brackets

The real power of generating functions appears when variables carry restrictions. Because each bracket simply lists the allowed values of its variable as exponents of \(x\), every restriction translates directly into editing that variable's bracket.

Let us examine each type of restriction carefully, paying close attention to the difference between the natural bound and imposed bounds.

Case 1: Lower Bound Only

Suppose a variable satisfies \(x_i \ge a_i\) (where \(a_i \ge 0\)) with no upper bound. Its smallest allowed value is \(a_i\), and its larger values can extend to \(\infty\). So its bracket is:

\[x^{a_i} + x^{a_i + 1} + x^{a_i + 2} + \dots = x^{a_i}(1 + x + x^2 + \dots) = \frac{x^{a_i}}{1 - x}\]

When all \(r\) variables satisfy \(x_i \ge a_i\) (\(a_i \ge 0\)), the product of brackets is:

\[\frac{x^{a_1}}{1 - x} \cdot \frac{x^{a_2}}{1 - x} \cdots \frac{x^{a_r}}{1 - x} = \frac{x^{a_1 + a_2 + \dots + a_r}}{(1 - x)^r}\]

Let \(A = a_1 + a_2 + \dots + a_r\). To find \([x^n]\,\frac{x^A}{(1 - x)^r}\), notice that multiplying any power series by \(x^A\) shifts every exponent up by \(A\). Hence:

\[[x^n]\left(x^A \cdot F(x)\right) = [x^{n - A}]\,F(x)\]

Therefore, for \(n' = n - A \ge 0\):

\[[x^n]\,\frac{x^A}{(1 - x)^r} = [x^{n'}]\,(1 - x)^{-r} = \binom{n' + r - 1}{r - 1}\]

Notice two important facts:

  • Factoring out \(x^{a_1 + \dots + a_r}\) is the exact algebraic mirror of the variable shift \(y_i = x_i - a_i\) from Section 07.
  • Lower bounds reduce the remaining sum to \(n' = n - (a_1 + \dots + a_r)\), which lowers the effective natural upper bound of every shifted variable \(y_i\) to \(n'\).

Example 2 (Positive and Negative Lower Bounds): Find the number of integer solutions to:

(a) \(x_1 + x_2 + x_3 = 12\) with \(x_1 \ge 2,\; x_2 \ge 3,\; x_3 \ge 1\).

(b) \(x_1 + x_2 + x_3 = 5\) with \(x_1 \ge -2,\; x_2 \ge -1,\; x_3 \ge 0\).

Solution:

(a) Write one bracket for each variable starting at its lower bound:

\[[x^{12}]\,(x^2 + x^3 + \dots)(x^3 + x^4 + \dots)(x^1 + x^2 + \dots) = [x^{12}]\,\frac{x^2}{1 - x} \cdot \frac{x^3}{1 - x} \cdot \frac{x^1}{1 - x}\]
\[= [x^{12}]\,\frac{x^6}{(1 - x)^3} = [x^{12 - 6}]\,(1 - x)^{-3} = [x^6]\,(1 - x)^{-3} = \binom{6 + 3 - 1}{3 - 1} = \binom{8}{2} = 28\]

(b) Here \(x_1\) and \(x_2\) can be negative! We must not write \((x^{-2} + x^{-1} + \dots)\) and blindly extend brackets to \(\infty\) without checking, because negative powers can cancel higher powers (for example, \(x^{-2} \cdot x^7 \cdot x^0 = x^5\) uses an exponent greater than \(5\)). Instead, shift every variable to become non-negative first:

\[y_1 = x_1 + 2 \ge 0, \qquad y_2 = x_2 + 1 \ge 0, \qquad y_3 = x_3 \ge 0\]

Substituting \(x_1 = y_1 - 2\), \(x_2 = y_2 - 1\), \(x_3 = y_3\) gives:

\[(y_1 - 2) + (y_2 - 1) + y_3 = 5 \implies y_1 + y_2 + y_3 = 8, \qquad y_1, y_2, y_3 \ge 0\]

Now all exponents are non-negative, so we safely apply generating functions:

\[[x^8]\,(1 - x)^{-3} = \binom{8 + 3 - 1}{3 - 1} = \binom{10}{2} = 45\]

Case 2: Upper Bound Only — Active vs. Redundant Bounds

Now suppose a variable has an imposed upper bound: \(0 \le x_i \le b_i\). Compare \(b_i\) with the equation's natural upper bound \(n\):

Subcase 2A: Active Upper Bound (\(b_i < n\)). Here the problem cuts off \(x_i\) strictly below its natural bound \(n\). We cannot extend this bracket to \(\infty\)! Why? Because the powers \(x^{b_i + 1}, x^{b_i + 2}, \dots, x^n\) are smaller than or equal to \(n\), so they would combine with terms from the other brackets to produce fake \(x^n\) terms corresponding to illegal solutions with \(x_i > b_i\).

Therefore, the bracket must stop at \(x^{b_i}\):

\[1 + x + x^2 + \dots + x^{b_i} = \frac{1 - x^{b_i + 1}}{1 - x}\]

Subcase 2B: Redundant Upper Bound (\(b_i \ge n\)). Here the imposed limit \(b_i\) is at least as large as the natural bound \(n\). Because all other variables are non-negative, \(x_i\) could never exceed \(n\) anyway—so the bound \(x_i \le b_i\) changes nothing! We may safely extend this bracket to \(\infty\) as \(\frac{1}{1 - x}\).

(Algebraic check: even if you kept the finite sum \(\frac{1 - x^{b_i + 1}}{1 - x}\) when \(b_i \ge n\), the extra term \(-x^{b_i + 1}\) has exponent \(b_i + 1 \ge n + 1 > n\), so it contributes zero to \([x^n]\)!)

Example 3 (Active vs. Redundant Upper Bounds): Find the number of non-negative integer solutions to:

\[x_1 + x_2 + x_3 = 10, \qquad 0 \le x_1 \le 4,\quad 0 \le x_2 \le 12,\quad 0 \le x_3 \le 10\]

Solution: The natural upper bound of this equation is \(n = 10\). Check each variable's upper bound against \(n = 10\):

  • \(x_1 \le 4 < 10\): active bound \(\longrightarrow\) bracket must stop at \(x^4\): \((1 + x + x^2 + x^3 + x^4) = \frac{1 - x^5}{1 - x}\).
  • \(x_2 \le 12 \ge 10\): redundant bound \(\longrightarrow\) behaves like no upper bound: \(\frac{1}{1 - x}\).
  • \(x_3 \le 10 \ge 10\): redundant bound \(\longrightarrow\) behaves like no upper bound: \(\frac{1}{1 - x}\).

Multiplying the three factors gives:

\[[x^{10}]\left(\frac{1 - x^5}{1 - x} \cdot \frac{1}{1 - x} \cdot \frac{1}{1 - x}\right) = [x^{10}]\,\frac{1 - x^5}{(1 - x)^3} = [x^{10}]\,(1 - x)^{-3} - [x^{10}]\,x^5 (1 - x)^{-3}\]

Using \([x^{10}]\,x^5 (1 - x)^{-3} = [x^{10 - 5}]\,(1 - x)^{-3} = [x^5]\,(1 - x)^{-3}\):

\[\binom{10 + 3 - 1}{2} - \binom{5 + 3 - 1}{2} = \binom{12}{2} - \binom{7}{2} = 66 - 21 = 45\]

Case 3: Both Lower and Upper Bounds, and Automatic Inclusion–Exclusion

Suppose a variable must satisfy both a lower bound and an upper bound:

\[a_i \le x_i \le b_i \qquad (0 \le a_i \le b_i)\]

Listing all allowed values from \(a_i\) up to \(b_i\) gives a finite geometric progression with \(b_i - a_i + 1\) terms:

\[x^{a_i} + x^{a_i + 1} + \dots + x^{b_i} = x^{a_i}\left(1 + x + x^2 + \dots + x^{b_i - a_i}\right) = x^{a_i}\,\frac{1 - x^{b_i - a_i + 1}}{1 - x}\]

Look closely at the exponent \(b_i - a_i + 1\). After factoring out \(x^{a_1 + \dots + a_r}\) from all brackets, the remaining target sum is:

\[n' = n - (a_1 + a_2 + \dots + a_r)\]

So the upper bound on \(x_i\) is active only if its allowed span \(b_i - a_i\) is strictly less than \(n'\) (that is, \(b_i - a_i < n'\)). If \(b_i - a_i \ge n'\), then the term \(-x^{b_i - a_i + 1}\) has degree \(> n'\) and automatically drops out!

Even better: when several variables have active upper bounds, multiplying out the numerator factors \(\prod (1 - x^{b_i - a_i + 1})\) automatically performs the Principle of Inclusion–Exclusion (Section 08)—without manually defining violation sets \(A_1, A_2, \dots\)!

Example 4 (Automatic PIE and Effective Bounds):

(a) Re-solve the three-bound equation from Section 08 using generating functions:

\[x_1 + x_2 + x_3 = 15, \qquad 0 \le x_1 \le 6,\quad 0 \le x_2 \le 7,\quad 0 \le x_3 \le 8\]

(b) Find the number of integer solutions to:

\[x_1 + x_2 + x_3 = 14, \qquad 2 \le x_1 \le 6,\quad 1 \le x_2 \le 5,\quad 3 \le x_3 \le 12\]

Solution:

(a) All three upper bounds are strictly less than \(15\), so all three are active. The count is:

\[[x^{15}]\left(\frac{1 - x^7}{1 - x} \cdot \frac{1 - x^8}{1 - x} \cdot \frac{1 - x^9}{1 - x}\right) = [x^{15}]\,\frac{(1 - x^7)(1 - x^8)(1 - x^9)}{(1 - x)^3}\]

Expand the numerator, dropping every term whose exponent already exceeds \(15\):

\[(1 - x^7)(1 - x^8)(1 - x^9) = 1 - x^7 - x^8 - x^9 + x^{15} + x^{16} + x^{17} - x^{24}\]

The terms \(x^{16}, x^{17}, -x^{24}\) all have degree \(> 15\) and contribute \(0\) to \([x^{15}]\). Retaining only terms of degree \(\le 15\):

\[[x^{15}]\,(1 - x^7 - x^8 - x^9 + x^{15})(1 - x)^{-3}\]
\[= [x^{15}](1 - x)^{-3} - [x^8](1 - x)^{-3} - [x^7](1 - x)^{-3} - [x^6](1 - x)^{-3} + [x^0](1 - x)^{-3}\]
\[= \binom{17}{2} - \binom{10}{2} - \binom{9}{2} - \binom{8}{2} + \binom{2}{2} = 136 - 45 - 36 - 28 + 1 = 28\]

Compare this line with Section 08: \(136\) is \(|U|\), \(45 + 36 + 28\) is \(|A_1| + |A_2| + |A_3|\), and \(+1\) is \(|A_1 \cap A_2|\)! Generating functions executed PIE automatically.

(b) Write the exact bracket for each variable:

\[[x^{14}]\,(x^2 + \dots + x^6)(x^1 + \dots + x^5)(x^3 + \dots + x^{12})\]

Factor out the lowest power from each bracket:

\[= [x^{14}]\,x^2 \cdot x^1 \cdot x^3 \cdot \frac{1 - x^{6 - 2 + 1}}{1 - x} \cdot \frac{1 - x^{5 - 1 + 1}}{1 - x} \cdot \frac{1 - x^{12 - 3 + 1}}{1 - x}\]
\[= [x^{14}]\,x^6 \cdot \frac{(1 - x^5)(1 - x^5)(1 - x^{10})}{(1 - x)^3} = [x^8]\,\frac{(1 - x^5)^2 (1 - x^{10})}{(1 - x)^3}\]

Look at \(x_3 \le 12\): before shifting, \(12 < 14\) looked like an active upper bound. However, once the lower bounds \(2 + 1 + 3 = 6\) reduced the remaining sum to \(n' = 8\), the span of \(x_3\) is \(12 - 3 = 9 \ge 8\), so the factor \((1 - x^{10})\) has \(x^{10}\) exceeding \(8\) and becomes redundant! Expanding only up to degree \(8\):

\[(1 - x^5)^2 (1 - x^{10}) = (1 - 2x^5 + x^{10})(1 - x^{10}) = 1 - 2x^5 + (\text{terms of degree} \ge 10)\]

Hence the number of solutions is:

\[[x^8]\,(1 - 2x^5)(1 - x)^{-3} = [x^8](1 - x)^{-3} - 2\,[x^3](1 - x)^{-3} = \binom{10}{2} - 2\binom{5}{2} = 45 - 20 = 25\]

Case 4: Parity, Multiples, and Arbitrary Allowed Sets

Unlike binary strings, generating functions handle gaps in allowed values effortlessly. You simply include only the allowed exponents inside that variable's bracket:

1. \(x_i\) is even (\(x_i \in \{0, 2, 4, 6, \dots\}\)):

\[1 + x^2 + x^4 + x^6 + \dots = \frac{1}{1 - x^2}\]

2. \(x_i\) is odd (\(x_i \in \{1, 3, 5, 7, \dots\}\)):

\[x + x^3 + x^5 + x^7 + \dots = x(1 + x^2 + x^4 + \dots) = \frac{x}{1 - x^2}\]

3. \(x_i\) is a non-negative multiple of \(k\) (\(x_i \in \{0, k, 2k, 3k, \dots\}\)):

\[1 + x^k + x^{2k} + x^{3k} + \dots = \frac{1}{1 - x^k}\]

4. \(x_i\) belongs to a specific finite set \(S\) (for example, \(x_i \in \{0, 2, 5\}\)):

\[x^0 + x^2 + x^5 = 1 + x^2 + x^5\]

Example 5: Find the number of non-negative integer solutions to \(x_1 + x_2 + x_3 = 10\) such that \(x_1\) is even, \(x_2\) is odd, and \(x_3 \in \{1, 3, 5\}\).

Solution: Encode each variable's allowed values directly into its bracket:

  • \(x_1\) even \(\longrightarrow 1 + x^2 + x^4 + \dots = \frac{1}{1 - x^2}\).
  • \(x_2\) odd \(\longrightarrow x + x^3 + x^5 + \dots = \frac{x}{1 - x^2}\).
  • \(x_3 \in \{1, 3, 5\} \longrightarrow x + x^3 + x^5\).

Multiply the three brackets and extract \([x^{10}]\):

\[[x^{10}]\left(\frac{1}{1 - x^2} \cdot \frac{x}{1 - x^2} \cdot (x + x^3 + x^5)\right) = [x^{10}]\,\frac{x^2 + x^4 + x^6}{(1 - x^2)^2}\]
\[= [x^8](1 - x^2)^{-2} + [x^6](1 - x^2)^{-2} + [x^4](1 - x^2)^{-2}\]

Set \(t = x^2\) so that \(x^{2k} = t^k\) and \((1 - x^2)^{-2} = (1 - t)^{-2} = \sum_{k=0}^{\infty} \binom{k + 1}{1} t^k = \sum_{k=0}^{\infty} (k + 1) t^k\). Then:

  • \([x^8](1 - x^2)^{-2} = [t^4](1 - t)^{-2} = 4 + 1 = 5\),
  • \([x^6](1 - x^2)^{-2} = [t^3](1 - t)^{-2} = 3 + 1 = 4\),
  • \([x^4](1 - x^2)^{-2} = [t^2](1 - t)^{-2} = 2 + 1 = 3\).

Adding these gives \(5 + 4 + 3 = 12\) solutions.


Case 5: Weighted Linear Equations (Coin-Change Problems)

Suppose variables have different positive integer coefficients:

\[c_1 x_1 + c_2 x_2 + \dots + c_r x_r = n, \qquad x_i \ge 0\]

Think carefully about what each variable contributes to the sum \(n\). Variable \(x_i\) does not add \(x_i\) to the total—it adds \(c_i x_i\)! As \(x_i\) runs through \(0, 1, 2, 3, \dots\), its contribution \(c_i x_i\) runs through \(0, c_i, 2c_i, 3c_i, \dots\).

Therefore, the bracket representing \(c_i x_i\) is:

\[1 + x^{c_i} + x^{2c_i} + x^{3c_i} + \dots = \frac{1}{1 - x^{c_i}}\]

When we pick \(x^{c_1 a_1}\) from bracket \(1\), \(x^{c_2 a_2}\) from bracket \(2\), \(\dots\), and \(x^{c_r a_r}\) from bracket \(r\), multiplying them gives \(x^{c_1 a_1 + c_2 a_2 + \dots + c_r a_r}\), so \([x^n]\) counts every solution of \(c_1 x_1 + \dots + c_r x_r = n\)!

Before solving coin-change and partition problems, we prove one small building-block formula that appears repeatedly whenever weights \(1\) and \(2\) occur together.

Lemma (Coefficient of \(x^m\) in \(\frac{1}{(1 - x)(1 - x^2)}\)): For every integer \(m \ge 0\):

\[[x^m]\,\frac{1}{(1 - x)(1 - x^2)} = \left\lfloor \frac{m}{2} \right\rfloor + 1\]

where \(\lfloor y \rfloor\) denotes the Greatest Integer Function (GIF)—the largest integer less than or equal to \(y\).

Proof: Expanding both factors gives \((1 + x + x^2 + \dots)(1 + x^2 + x^4 + \dots)\). Thus \([x^m]\,\frac{1}{(1 - x)(1 - x^2)}\) counts all non-negative integer solutions \((a, b)\) to:

\[a + 2b = m, \qquad a \ge 0,\; b \ge 0\]

Notice that once we choose the integer \(b\), the value of \(a\) is uniquely forced to be \(a = m - 2b\). Since the first bracket contains every non-negative power of \(x\), this value of \(a\) is valid if and only if \(a \ge 0\), which means:

\[m - 2b \ge 0 \iff 2b \le m \iff 0 \le b \le \frac{m}{2}\]

Now count how many integers \(b\) satisfy \(0 \le b \le \frac{m}{2}\) by checking the two cases:

Even \(m\) (say \(m = 2k\)): Here \(\frac{m}{2} = k\) is already an integer, so \(b\) can take any value in \(\{0, 1, 2, \dots, k\}\). For example, if \(m = 10\), then \(0 \le b \le 5\), giving \(b \in \{0, 1, 2, 3, 4, 5\}\)—a total of \(5 + 1 = 6\) values. In general, counting from \(0\) to \(k\) inclusive gives \(k + 1 = \frac{m}{2} + 1\) values.

Odd \(m\) (say \(m = 2k + 1\)): Here \(\frac{m}{2} = k + 0.5\) is not an integer. Because \(b\) must be an integer, \(b \le k + 0.5\) forces \(b \le k = \left\lfloor \frac{m}{2} \right\rfloor\). For example, if \(m = 5\), then \(0 \le b \le 2.5\), so the largest allowed integer is \(\lfloor 2.5 \rfloor = 2\), giving \(b \in \{0, 1, 2\}\)—a total of \(2 + 1 = 3\) values. In general, \(b \in \{0, 1, 2, \dots, k\}\) gives \(k + 1 = \left\lfloor \frac{m}{2} \right\rfloor + 1\) values.

In both even and odd cases, the largest integer \(b \le \frac{m}{2}\) is \(\left\lfloor \frac{m}{2} \right\rfloor\), so \(b \in \left\{0, 1, 2, \dots, \left\lfloor \frac{m}{2} \right\rfloor\right\}\). Including \(b = 0\) adds \(1\) to the count, giving exactly \(\left\lfloor \frac{m}{2} \right\rfloor + 1\) solutions. \(\blacksquare\)

(Quick check: multiplying out the first few terms gives \(\frac{1}{(1 - x)(1 - x^2)} = 1 + x + 2x^2 + 2x^3 + 3x^4 + 3x^5 + \dots\), whose coefficients \(1, 1, 2, 2, 3, 3, \dots\) match \(\lfloor m/2 \rfloor + 1\) for every \(m\).)

Example 6 (Making Change with Coins): In how many ways can a sum of ₹\(10\) be formed using unlimited ₹\(1\), ₹\(2\), and ₹\(5\) coins?

Solution: Let \(x_1, x_2, x_3 \ge 0\) be the numbers of ₹\(1\), ₹\(2\), and ₹\(5\) coins used. We want the number of non-negative integer solutions to:

\[1 \cdot x_1 + 2 \cdot x_2 + 5 \cdot x_3 = 10\]

The generating function is:

\[[x^{10}]\,\frac{1}{(1 - x)(1 - x^2)(1 - x^5)}\]

First expand the factor with the largest step, \(\frac{1}{1 - x^5} = 1 + x^5 + x^{10} + \dots\) (dropping powers \(> 10\)):

\[[x^{10}]\,\frac{1 + x^5 + x^{10}}{(1 - x)(1 - x^2)} = [x^{10}]\,\frac{1}{(1 - x)(1 - x^2)} + [x^5]\,\frac{1}{(1 - x)(1 - x^2)} + [x^0]\,\frac{1}{(1 - x)(1 - x^2)}\]

Applying our Lemma \([x^m]\,\frac{1}{(1 - x)(1 - x^2)} = \left\lfloor \frac{m}{2} \right\rfloor + 1\) to \(m = 10, 5, 0\):

  • \(m = 10\) (\(x_3 = 0\)): \(\lfloor 10/2 \rfloor + 1 = 5 + 1 = 6\),
  • \(m = 5\) (\(x_3 = 1\)): \(\lfloor 5/2 \rfloor + 1 = 2 + 1 = 3\),
  • \(m = 0\) (\(x_3 = 2\)): \(\lfloor 0/2 \rfloor + 1 = 0 + 1 = 1\).

Total number of ways \(= 6 + 3 + 1 = 10\).


Formal Definition and Further Applications

Now that we have built the full mechanism from scratch, we state the formal definition.

Definition (Ordinary Generating Function): Given a sequence of numbers \((a_0, a_1, a_2, \dots)\), its Ordinary Generating Function (OGF) is the formal power series:

\[G(x) = a_0 + a_1 x + a_2 x^2 + a_3 x^3 + \dots = \sum_{n=0}^{\infty} a_n x^n\]

In this compact language, the single function \(G(x)\) stores the entire infinite sequence of answers \(a_0, a_1, a_2, \dots\) as its coefficients, and \(a_n = [x^n]\,G(x)\). For example, the OGF for the sequence \(a_n = \binom{n + r - 1}{r - 1}\) is \(G(x) = (1 - x)^{-r}\).

Below are five classic applications showing how broadly OGFs work across combinatorics.


Application 1: Dice Sums and Capped Selections

Example 7 (Sum of Faces on Three Dice): Three distinct six-sided dice (labeled \(1\) through \(6\)) are rolled. In how many ways can the sum of the three outcomes equal \(12\)?

Solution: Let \(x_1, x_2, x_3\) be the numbers showing on the first, second, and third dice. We need integer solutions to:

\[x_1 + x_2 + x_3 = 12, \qquad 1 \le x_1, x_2, x_3 \le 6\]

Each die contributes the bracket \((x + x^2 + x^3 + x^4 + x^5 + x^6) = x\,\frac{1 - x^6}{1 - x}\). Multiplying all three brackets:

\[[x^{12}]\,\left(x\,\frac{1 - x^6}{1 - x}\right)^3 = [x^{12}]\,x^3\,\frac{(1 - x^6)^3}{(1 - x)^3} = [x^9]\,(1 - 3x^6 + 3x^{12} - x^{18})(1 - x)^{-3}\]

Drop terms of degree \(> 9\) from \((1 - x^6)^3\), leaving only \(1 - 3x^6\):

\[[x^9]\,(1 - x)^{-3} - 3\,[x^3]\,(1 - x)^{-3} = \binom{9 + 3 - 1}{2} - 3\binom{3 + 3 - 1}{2} = \binom{11}{2} - 3\binom{5}{2} = 55 - 30 = 25\]

Application 2: Linear Inequalities via a Slack Variable

How do we solve an inequality such as \(x_1 + x_2 + \dots + x_r \le n\) using generating functions? Just as in Section 07, introduce a non-negative slack variable \(s = n - (x_1 + x_2 + \dots + x_r) \ge 0\), converting the inequality into the exact equation:

\[x_1 + x_2 + \dots + x_r + s = n, \qquad s \ge 0\]

Since \(s \ge 0\) has no upper bound, it simply multiplies the generating function by one extra factor \(\frac{1}{1 - x}\)!

Example 8: Find the number of non-negative integer solutions to \(x_1 + x_2 + x_3 \le 10\) subject to \(0 \le x_1 \le 4\).

Solution: Introduce \(s \ge 0\) so that \(x_1 + x_2 + x_3 + s = 10\). The variables contribute \(\frac{1 - x^5}{1 - x}\) for \(x_1\), and \(\frac{1}{1 - x}\) for each of \(x_2, x_3, s\):

\[[x^{10}]\,\frac{1 - x^5}{(1 - x)^4} = [x^{10}](1 - x)^{-4} - [x^5](1 - x)^{-4}\]
\[= \binom{10 + 4 - 1}{3} - \binom{5 + 4 - 1}{3} = \binom{13}{3} - \binom{8}{3} = 286 - 56 = 230\]

Application 3: Ordering Restrictions and Integer Partitions

Suppose variables are constrained by an ordering such as \(0 \le x_1 \le x_2 \le x_3\). At first glance, the variables seem coupled—you cannot write independent brackets for \(x_1, x_2, x_3\) directly.

The key idea is to switch to non-negative differences (gaps):

\[d_1 = x_1 \ge 0, \qquad d_2 = x_2 - x_1 \ge 0, \qquad d_3 = x_3 - x_2 \ge 0\]

Expressing \(x_1, x_2, x_3\) in terms of the independent variables \(d_1, d_2, d_3\):

\[x_1 = d_1, \qquad x_2 = d_1 + d_2, \qquad x_3 = d_1 + d_2 + d_3\]

Now the sum \(x_1 + x_2 + x_3 = n\) transforms into a weighted equation in independent non-negative variables:

\[d_1 + (d_1 + d_2) + (d_1 + d_2 + d_3) = n \implies 3 d_1 + 2 d_2 + 1 d_3 = n, \qquad d_1, d_2, d_3 \ge 0\]

Example 9 (Ordered Triples and Integer Partitions): Find the number of integer solutions to \(x_1 + x_2 + x_3 = 9\) with \(0 \le x_1 \le x_2 \le x_3\). (Equivalently, count the number of ways to distribute \(9\) identical balls into \(3\) identical boxes—the integer partitions of \(9\) into at most \(3\) parts from Section 09!)

Solution: Substituting \(x_1 = d_1,\; x_2 = d_1 + d_2,\; x_3 = d_1 + d_2 + d_3\) turns the problem into \(3 d_1 + 2 d_2 + d_3 = 9\) with \(d_1, d_2, d_3 \ge 0\). Its generating function is:

\[[x^9]\,\frac{1}{(1 - x)(1 - x^2)(1 - x^3)}\]

Expand \(\frac{1}{1 - x^3} = 1 + x^3 + x^6 + x^9 + \dots\) (ignoring powers \(> 9\)) and apply our Lemma \([x^m]\,\frac{1}{(1 - x)(1 - x^2)} = \lfloor m/2 \rfloor + 1\):

\[[x^9]\,\frac{1 + x^3 + x^6 + x^9}{(1 - x)(1 - x^2)} = \left(\left\lfloor \frac{9}{2} \right\rfloor + 1\right) + \left(\left\lfloor \frac{6}{2} \right\rfloor + 1\right) + \left(\left\lfloor \frac{3}{2} \right\rfloor + 1\right) + \left(\left\lfloor \frac{0}{2} \right\rfloor + 1\right)\]
\[= 5 + 4 + 2 + 1 = 12\]

Connection to Section 09 (Euler's Generating Function for Integer Partitions):
An unrestricted integer partition of \(n\) chooses \(c_1 \ge 0\) parts of size \(1\), \(c_2 \ge 0\) parts of size \(2\), \(c_3 \ge 0\) parts of size \(3\), and so on, such that \(1 \cdot c_1 + 2 \cdot c_2 + 3 \cdot c_3 + \dots = n\). Hence the OGF for the total partition number \(p(n)\) is Euler's famous infinite product: \(\sum_{n=0}^{\infty} p(n) x^n = \prod_{k=1}^{\infty} \frac{1}{1 - x^k}\).


Application 4: Non-Congruent Integer Triangles (Alcuin's Sequence)

One of the most striking uses of generating functions is counting distinct (non-congruent) triangles with integer side lengths and perimeter \(n\), denoted \(T(n)\). The sequence \(T(0), T(1), T(2), \dots\) is known as Alcuin's sequence.

Because triangles with the same three side lengths are congruent regardless of how the sides are labeled, we list the sides in non-decreasing order:

\[1 \le a \le b \le c, \qquad a + b + c = n\]

For \((a, b, c)\) to form a genuine (non-degenerate) triangle, it must also satisfy the Triangle Inequality. Since \(a \le b \le c\), the inequalities \(a + c > b\) and \(b + c > a\) hold automatically; the only extra condition we need is:

\[a + b > c \iff a + b - c \ge 1\]

(because \(a, b, c\) are integers).

Step 1: Converting Coupled Inequalities into Independent Variables

Just as in Application 3, replace the coupled side lengths \((a, b, c)\) with three independent non-negative integers measuring the triangle-inequality excess and the two side gaps:

\[u = (a + b - c) - 1 \ge 0, \qquad v = b - a \ge 0, \qquad w = c - b \ge 0\]

Now solve for \(a, b, c\) in terms of \(u, v, w\). From \(v = b - a\) and \(w = c - b\), we have \(b = a + v\) and \(c = b + w = a + v + w\). Substituting into \(u = a + b - c - 1\):

\[u = a + (a + v) - (a + v + w) - 1 = a - w - 1 \implies a = u + w + 1\]

Hence:

\[a = u + w + 1, \qquad b = u + v + w + 1, \qquad c = u + v + 2w + 1\]

Verify the bijection in one glance: for every choice of non-negative integers \((u, v, w) \ge 0\), we automatically get \(a \ge 1\), \(b - a = v \ge 0\), \(c - b = w \ge 0\), and \(a + b - c = u + 1 \ge 1\). Conversely, every valid triangle \((a, b, c)\) determines a unique triple \((u, v, w) \ge 0\).

Encoding an Integer Triangle via Three Independent Non-Negative Gaps

Now substitute \(a, b, c\) into the perimeter equation \(a + b + c = n\):

\[(u + w + 1) + (u + v + w + 1) + (u + v + 2w + 1) = n\]
\[2v + 3u + 4w = n - 3, \qquad u, v, w \ge 0\]

This is a weighted linear equation with weights \(2, 3, 4\)! Therefore, the Ordinary Generating Function for Alcuin's sequence \(T(n)\) is:

\[\sum_{n=0}^{\infty} T(n) x^n = \frac{x^3}{(1 - x^2)(1 - x^3)(1 - x^4)}\]

Step 2: Separating Even and Odd Perimeters

Multiply both numerator and denominator by \((1 + x^3)\) and use \((1 - x^3)(1 + x^3) = 1 - x^6\):

\[\sum_{n=0}^{\infty} T(n) x^n = \frac{x^3(1 + x^3)}{(1 - x^2)(1 - x^4)(1 - x^6)} = \frac{x^3}{(1 - x^2)(1 - x^4)(1 - x^6)} + \frac{x^6}{(1 - x^2)(1 - x^4)(1 - x^6)}\]

Notice that every power of \(x\) in \(\frac{1}{(1 - x^2)(1 - x^4)(1 - x^6)}\) is even! Thus:

  • The first term \(\frac{x^3}{(1 - x^2)(1 - x^4)(1 - x^6)}\) supplies all odd-perimeter terms \(T(3), T(5), T(7), \dots\)
  • The second term \(\frac{x^6}{(1 - x^2)(1 - x^4)(1 - x^6)}\) supplies all even-perimeter terms \(T(6), T(8), T(10), \dots\)

This immediately reveals two remarkable facts:

1. Parity Symmetry: For every odd integer \(n \ge 1\), the coefficient of \(x^n\) in the first term equals the coefficient of \(x^{n + 3}\) in the second term:

\[T(n) = T(n + 3) \qquad (\text{for every odd } n \ge 1)\]

(Geometric reason: adding \(1\) to every side maps \((a, b, c) \mapsto (a + 1, b + 1, c + 1)\), giving a bijection between odd-perimeter triangles of perimeter \(n\) and even-perimeter triangles of perimeter \(n + 3\)! In reverse, if \(a + b + c\) is even, then \(a + b - c\) is also even and thus \(\ge 2\), and \(a \ge 2\) automatically, so subtracting \(1\) from every side always keeps a valid triangle.)

2. Direct Link to Partitions into at Most \(3\) Parts: Setting \(t = x^2\) inside \(\frac{1}{(1 - x^2)(1 - x^4)(1 - x^6)}\) turns it into \(\frac{1}{(1 - t)(1 - t^2)(1 - t^3)}\)—the exact generating function from Application 3! Letting \(P_3(k) = [t^k]\,\frac{1}{(1 - t)(1 - t^2)(1 - t^3)}\):

\[T(2m) = P_3(m - 3) \quad (m \ge 3), \qquad T(2m - 3) = P_3(m - 3) \quad (m \ge 3)\]

Step 3: Alcuin's Closed-Form Formula

We can find a closed formula for \(P_3(k)\)—and hence for \(T(n)\)—using only elementary algebra!

Theorem (Nearest-Integer Formula for \(P_3(k)\)): For every integer \(k \ge 0\):

\[P_3(k) = [t^k]\,\frac{1}{(1 - t)(1 - t^2)(1 - t^3)} = \operatorname{round}\!\left(\frac{(k + 3)^2}{12}\right)\]

where \(\operatorname{round}(y)\) denotes the integer nearest to \(y\).

Proof: Multiply \(\sum_{k=0}^{\infty} P_3(k) t^k = \frac{1}{(1 - t)(1 - t^2)(1 - t^3)}\) by \((1 - t^6)\). Factor \(1 - t^6 = (1 - t^3)(1 + t^3)\), cancel \((1 - t^3)\), then factor \(1 + t^3 = (1 + t)(1 - t + t^2)\) and \(1 - t^2 = (1 - t)(1 + t)\) to cancel \((1 + t)\):

\[(1 - t^6)\sum_{k=0}^{\infty} P_3(k) t^k = \frac{1 + t^3}{(1 - t)(1 - t^2)} = \frac{1 - t + t^2}{(1 - t)^2} = \frac{(1 - t)^2 + t}{(1 - t)^2} = 1 + \frac{t}{(1 - t)^2}\]

Since \((1 - t)^{-2} = \sum_{j=0}^{\infty} (j + 1) t^j\), multiplying by \(t\) gives \(\frac{t}{(1 - t)^2} = \sum_{k=1}^{\infty} k\,t^k\). Comparing \([t^k]\) on both sides for every \(k \ge 6\) yields the step-\(6\) recurrence:

\[P_3(k) - P_3(k - 6) = k \qquad (k \ge 6)\]

Now test the quadratic function \(Q(k) = \frac{(k + 3)^2}{12}\). Its step-\(6\) difference is:

\[Q(k) - Q(k - 6) = \frac{(k + 3)^2 - (k - 3)^2}{12} = \frac{12k}{12} = k\]

Because \(P_3(k)\) and \(Q(k)\) grow by the exact same amount \(k\) whenever \(k\) increases by \(6\), their difference \(E(k) = Q(k) - P_3(k)\) satisfies \(E(k) = E(k - 6)\) for all \(k \ge 6\)—it repeats periodically every \(6\) steps! Check the first \(6\) values directly (computing \(P_3(k)\) via our Lemma as in Example 9):

\(k\) \(0\) \(1\) \(2\) \(3\) \(4\) \(5\)
\(P_3(k)\) \(1\) \(1\) \(2\) \(3\) \(4\) \(5\)
\(Q(k) = (k + 3)^2 / 12\) \(9/12\) \(16/12\) \(25/12\) \(36/12\) \(49/12\) \(64/12\)
Error \(\lvert Q(k) - P_3(k) \rvert\) \(1/4\) \(1/3\) \(1/12\) \(0\) \(1/12\) \(1/3\)

For every \(k \in \{0, 1, 2, 3, 4, 5\}\)—and therefore for every integer \(k \ge 0\)—the error satisfies \(\lvert Q(k) - P_3(k) \rvert \le \frac{1}{3} < \frac{1}{2}\). Hence \(P_3(k)\) is always the unique integer nearest to \(\frac{(k + 3)^2}{12}\). \(\blacksquare\)

Substituting \(k = \frac{n}{2} - 3\) (so \(k + 3 = \frac{n}{2}\)) for even \(n\), and \(k = \frac{n - 3}{2}\) (so \(k + 3 = \frac{n + 3}{2}\)) for odd \(n\), gives Alcuin's Theorem immediately (and checking \(n = 1, 2, 3, 4\) shows it holds for all \(n \ge 1\)):

Theorem (Alcuin's Formula for Distinct Integer Triangles): The number \(T(n)\) of non-congruent triangles with integer side lengths and perimeter \(n \ge 1\) is:

\[T(n) = \begin{cases} \operatorname{round}\!\left(\dfrac{n^2}{48}\right) & \text{if } n \text{ is even,} \\[10pt] \operatorname{round}\!\left(\dfrac{(n + 3)^2}{48}\right) & \text{if } n \text{ is odd.} \end{cases}\]

Example 10 (Counting Distinct Integer Triangles): Find the number of non-congruent triangles with integer side lengths having perimeter: (a) \(n = 15\), (b) \(n = 20\).

Solution:

(a) Perimeter \(n = 15\) (odd):

Method 1 (Direct Coefficient Extraction): Since \(n = 15\) is odd, write \(15 = 2m - 3 \implies m = 9\), so \(k = m - 3 = 6\):

\[T(15) = P_3(6) = [t^6]\,\frac{1}{(1 - t)(1 - t^2)(1 - t^3)} = [t^6]\,\frac{1 + t^3 + t^6}{(1 - t)(1 - t^2)}\]

Applying our Lemma \([t^r]\,\frac{1}{(1 - t)(1 - t^2)} = \lfloor r/2 \rfloor + 1\) for \(r = 6, 3, 0\):

\[T(15) = \left(\left\lfloor \frac{6}{2} \right\rfloor + 1\right) + \left(\left\lfloor \frac{3}{2} \right\rfloor + 1\right) + \left(\left\lfloor \frac{0}{2} \right\rfloor + 1\right) = 4 + 2 + 1 = 7\]

Method 2 (Alcuin's Formula): Since \(n = 15\) is odd:

\[T(15) = \operatorname{round}\!\left(\frac{(15 + 3)^2}{48}\right) = \operatorname{round}\!\left(\frac{324}{48}\right) = \operatorname{round}(6.75) = 7\]

(Indeed, the \(7\) triangles are \((1, 7, 7),\; (2, 6, 7),\; (3, 5, 7),\; (3, 6, 6),\; (4, 4, 7),\; (4, 5, 6),\; (5, 5, 5)\).)

(b) Perimeter \(n = 20\) (even): Applying Alcuin's Formula directly:

\[T(20) = \operatorname{round}\!\left(\frac{20^2}{48}\right) = \operatorname{round}\!\left(\frac{400}{48}\right) = \operatorname{round}(8.33\dots) = 8\]

Application 5: Proving Binomial Identities by Comparing Coefficients

Two polynomials (or formal power series) are identical if and only if they have the exact same coefficient of \(x^r\) for every \(r \ge 0\). Comparing \([x^r]\) on both sides of a simple algebraic identity often proves deep combinatorial identities in two lines!

Example 11 (Algebraic Proof of Vandermonde's Identity): For non-negative integers \(m, n, r\), prove that:

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

Proof: Start from the elementary polynomial identity:

\[(1 + x)^m (1 + x)^n = (1 + x)^{m + n}\]

Compare \([x^r]\) on both sides:

Right-hand side: By the Binomial Theorem, \([x^r]\,(1 + x)^{m + n} = \binom{m + n}{r}\).

Left-hand side: Expand both factors using the Binomial Theorem:

\[(1 + x)^m (1 + x)^n = \left(\sum_{k=0}^{m} \binom{m}{k} x^k\right)\left(\sum_{j=0}^{n} \binom{n}{j} x^j\right)\]

To obtain \(x^r\) by picking one term from each bracket, pick \(\binom{m}{k} x^k\) from the first bracket (for any \(0 \le k \le r\)) and the matching term \(\binom{n}{r - k} x^{r - k}\) from the second bracket (with the convention \(\binom{p}{q} = 0\) if \(q > p\)). Summing over all \(k = 0, 1, \dots, r\) gives:

\[[x^r]\,(1 + x)^m (1 + x)^n = \sum_{k=0}^{r} \binom{m}{k}\binom{n}{r - k}\]

Equating the two expressions for \([x^r]\) completes the proof. \(\blacksquare\)

(Compare this two-line generating-function proof with the committee-selection combinatorial proof we gave in Section 04!)


Summary Reference: Translating Restrictions into Factors

For any linear equation with target sum \(n\) (and all shifted variables non-negative), translate each variable's condition into its exact generating factor:

Variable Condition Allowed Exponents Exact Factor Notes
\(x_i \ge 0\) (no upper bound) \(0, 1, 2, \dots\) \(\dfrac{1}{1 - x}\) Natural bound \(n\); safe to extend to \(\infty\)
\(x_i \ge a_i\) (\(a_i \ge 0\), no upper bound) \(a_i, a_i + 1, \dots\) \(\dfrac{x^{a_i}}{1 - x}\) Lowers remaining budget to \(n' = n - \sum a_j\)
\(x_i \ge -c_i\) (negative lower bound) Shift \(y_i = x_i + c_i \ge 0\) \(\dfrac{1}{1 - x}\) (for \(y_i\)) Always shift first before extending to \(\infty\)
\(0 \le x_i \le b_i\) (\(b_i < n\), active) \(0, 1, \dots, b_i\) \(\dfrac{1 - x^{b_i + 1}}{1 - x}\) Must stop at \(x^{b_i}\); cannot extend to \(\infty\)
\(0 \le x_i \le b_i\) (\(b_i \ge n\), redundant) \(0, 1, \dots, n, \dots\) \(\dfrac{1}{1 - x}\) Natural bound \(n\) already enforces \(x_i \le b_i\)
\(a_i \le x_i \le b_i\) (\(0 \le a_i \le b_i\)) \(a_i, a_i + 1, \dots, b_i\) \(x^{a_i}\,\dfrac{1 - x^{b_i - a_i + 1}}{1 - x}\) Active only when span \(b_i - a_i < n'\)
\(x_i \ge 0\) even \(0, 2, 4, \dots\) \(\dfrac{1}{1 - x^2}\) Step size \(2\), starting at \(0\)
\(x_i \ge 1\) odd \(1, 3, 5, \dots\) \(\dfrac{x}{1 - x^2}\) Step size \(2\), starting at \(1\)
\(x_i \ge 0\) multiple of \(k\) (or weight \(k x_i\)) \(0, k, 2k, \dots\) \(\dfrac{1}{1 - x^k}\) Used for divisibility and weighted sums
\(x_i \in S\) (finite set) Elements of \(S\) \(\sum_{s \in S} x^s\) Direct list of allowed exponents
Inequality \(x_1 + \dots + x_r \le n\) Add slack \(s \ge 0\) Multiply by \(\dfrac{1}{1 - x}\) Converts \(\le n\) into \(= n\)