# Multinomial Theorem and Exponential Generating Functions

> The multinomial theorem, why ordinary generating functions fail for arrangements, and exponential generating functions for counting ordered selections.

- Source: https://matherama.com/tutorials/chapter-27-combinatorics/12-multinomial-theorem-and-exponential-generating-functions/
- Topic: Combinatorics (https://matherama.com/tutorials/chapter-27-combinatorics/)
- Author: Ankit Kumar Chauhan (B.Tech, Electrical Engineering, IIT Kharagpur)
- Updated: 2026-11-29
- License: free to read; please cite the source URL when quoting.
- Math notation: LaTeX — inline \( … \) or $ … $, display \[ … \] or $$ … $$.

---
In Section 11, we started from the basic rule of expanding algebraic brackets and built **Ordinary Generating Functions (OGFs)** to count *unordered* selections with repetition.

Now we ask two natural follow-up questions:

1. When we expand $(x_1 + x_2 + \dots + x_r)^n$ by picking one term from each bracket, what is the exact coefficient of $x_1^{a_1} x_2^{a_2} \cdots x_r^{a_r}$? This gives the **Multinomial Theorem**.
2. What if **order matters**—such as counting $4$-letter words from **MISSISSIPPI**, $n$-digit strings with parity rules, or distributing $n$ distinct balls into $r$ distinct boxes? Answering this leads directly to **Exponential Generating Functions (EGFs)**.

---

## The Multinomial Theorem

Return to the bracket-picking rule from Section 11, and look closely at the cube of a three-term sum:

$$(a + b + c)^3 = (a + b + c)(a + b + c)(a + b + c)$$

To form one raw product in this expansion, we pick **exactly one letter from Bracket 1, one letter from Bracket 2, and one letter from Bracket 3**, and multiply them together. Before combining like terms, there are $3 \times 3 \times 3 = 27$ raw products.

Now ask: *how many of these $27$ raw products simplify to $a^2 b$?*

To get $a^2 b$, we must pick `a` from **two** of the three brackets and `b` from the remaining **one** bracket. Listing every valid pick explicitly:

- Bracket 1: `a`, Bracket 2: `a`, Bracket 3: `b` $\longrightarrow$ raw word `aab` $\longrightarrow a^2 b$,
- Bracket 1: `a`, Bracket 2: `b`, Bracket 3: `a` $\longrightarrow$ raw word `aba` $\longrightarrow a^2 b$,
- Bracket 1: `b`, Bracket 2: `a`, Bracket 3: `a` $\longrightarrow$ raw word `baa` $\longrightarrow a^2 b$.

There are exactly **$3$** ways—because forming $a^2 b$ is identical to arranging the $3$-letter multiset $\{2 \cdot a,\; 1 \cdot b\}$ across the $3$ numbered brackets! By the **Multiset Permutation Theorem** (Section 06), this count is $\frac{3!}{2!\,1!} = 3$.

![Why the Coefficient of a2b in (a+b+c)3 Equals 3!/(2!1!) = 3](https://matherama.com/content-assets/tutorials/chapter-27-combinatorics/multinomial_bracket_picks.png)

Similarly, to get $abc$, we pick one `a`, one `b`, and one `c` across the $3$ brackets (`abc, acb, bac, bca, cab, cba`), giving $\frac{3!}{1!\,1!\,1!} = 6$ ways.

### Statement and Proof of the Multinomial Theorem

The exact same reasoning works for $r$ variables raised to any positive integer power $n$.

**Theorem (The Multinomial Theorem):** For any positive integer $n$ and variables $x_1, x_2, \dots, x_r$:

$$(x_1 + x_2 + \dots + x_r)^n = \sum_{\substack{a_1 + a_2 + \dots + a_r = n \\ a_1, a_2, \dots, a_r \ge 0}} \frac{n!}{a_1!\,a_2!\cdots a_r!}\, x_1^{a_1} x_2^{a_2} \cdots x_r^{a_r}$$

**Proof:** Write $(x_1 + x_2 + \dots + x_r)^n$ as the product of $n$ identical numbered brackets:

$$\underbrace{(x_1 + x_2 + \dots + x_r)(x_1 + x_2 + \dots + x_r)\cdots(x_1 + x_2 + \dots + x_r)}_{n \text{ brackets}}$$

Every raw product picks one variable from each of the $n$ brackets, so the sum of its exponents must equal $n$: $a_1 + a_2 + \dots + a_r = n$ with each $a_i \ge 0$.

Fix one exponent tuple $(a_1, a_2, \dots, a_r)$ summing to $n$. A raw product equals $x_1^{a_1} x_2^{a_2} \cdots x_r^{a_r}$ if and only if we pick $x_1$ from $a_1$ brackets, $x_2$ from $a_2$ brackets, $\dots$, and $x_r$ from $a_r$ brackets. Recording which variable is picked from Bracket $1$, Bracket $2$, $\dots$, Bracket $n$ creates an exact bijection with all distinct linear permutations of the multiset $\{a_1 \cdot x_1,\; a_2 \cdot x_2,\; \dots,\; a_r \cdot x_r\}$.

By the **Multiset Permutation Theorem** (Section 06), the number of such permutations—and hence the coefficient of $x_1^{a_1} x_2^{a_2} \cdots x_r^{a_r}$—is $\frac{n!}{a_1!\,a_2!\cdots a_r!}$. Summing over all valid tuples $(a_1, \dots, a_r)$ completes the proof. $\blacksquare$

The number $\frac{n!}{a_1!\,a_2!\cdots a_r!}$ is called a **multinomial coefficient** and is often written:

$$\binom{n}{a_1,\; a_2,\; \dots,\; a_r} = \frac{n!}{a_1!\,a_2!\cdots a_r!}$$

Notice three immediate consequences:

**1. Special case $r = 2$ (Binomial Theorem):** Setting $r = 2$ with $a_1 = k$ and $a_2 = n - k$ gives $\binom{n}{k,\; n - k} = \frac{n!}{k!\,(n - k)!} = \binom{n}{k}$, recovering $(x_1 + x_2)^n = \sum_{k=0}^{n} \binom{n}{k} x_1^k x_2^{n - k}$.

**2. Sum of all multinomial coefficients:** Setting $x_1 = x_2 = \dots = x_r = 1$ yields:

$$\sum_{\substack{a_1 + \dots + a_r = n \\ a_i \ge 0}} \frac{n!}{a_1!\,a_2!\cdots a_r!} = r^n$$

*(Combinatorial meaning: partitioning all $r^n$ ways of placing $n$ distinct balls into $r$ distinct boxes by the exact box sizes $(a_1, \dots, a_r)$ from Section 09!)*

**3. Number of distinct terms:** Each distinct term corresponds to one non-negative integer solution of $a_1 + a_2 + \dots + a_r = n$, so $(x_1 + x_2 + \dots + x_r)^n$ has exactly $\binom{n + r - 1}{r - 1}$ distinct terms (matching Section 07).

**Example 1:** Find the coefficient of $x^3 y^2 z^2$ in the expansion of $(2x - y + 3z)^7$.

**Solution:** Apply the Multinomial Theorem with $x_1 = 2x$, $x_2 = -y$, $x_3 = 3z$, and $n = 7$. The general term is:

$$\frac{7!}{a_1!\,a_2!\,a_3!}\,(2x)^{a_1} (-y)^{a_2} (3z)^{a_3} = \frac{7!}{a_1!\,a_2!\,a_3!}\,2^{a_1} (-1)^{a_2} 3^{a_3}\, x^{a_1} y^{a_2} z^{a_3}$$

For $x^3 y^2 z^2$, we need $a_1 = 3$, $a_2 = 2$, $a_3 = 2$ (check: $3 + 2 + 2 = 7$). Substituting these values gives:

$$\frac{7!}{3!\,2!\,2!} \cdot 2^3 \cdot (-1)^2 \cdot 3^2 = 210 \times 8 \times 1 \times 9 = 15{,}120$$

**Example 2 (Multiple Tuples Contributing to the Same Power):** Find the coefficient of $x^4$ in the expansion of $(1 + 2x - x^2)^5$.

**Solution:** Be careful! Because $x$ appears in *more than one* term inside the bracket, different exponent tuples can produce the same power $x^4$. Write the general term using $a_1 + a_2 + a_3 = 5$ ($a_1, a_2, a_3 \ge 0$):

$$\frac{5!}{a_1!\,a_2!\,a_3!}\,(1)^{a_1} (2x)^{a_2} (-x^2)^{a_3} = \frac{5!}{a_1!\,a_2!\,a_3!}\,2^{a_2} (-1)^{a_3}\, x^{a_2 + 2a_3}$$

We need all non-negative integer triples $(a_1, a_2, a_3)$ satisfying both $a_1 + a_2 + a_3 = 5$ and $a_2 + 2a_3 = 4$. List by $a_3 \in \{0, 1, 2\}$:

- **$a_3 = 0 \implies a_2 = 4,\; a_1 = 1$:** $\frac{5!}{1!\,4!\,0!}\,2^4 (-1)^0 = 5 \times 16 = 80$.
- **$a_3 = 1 \implies a_2 = 2,\; a_1 = 2$:** $\frac{5!}{2!\,2!\,1!}\,2^2 (-1)^1 = 30 \times 4 \times (-1) = -120$.
- **$a_3 = 2 \implies a_2 = 0,\; a_1 = 3$:** $\frac{5!}{3!\,0!\,2!}\,2^0 (-1)^2 = 10 \times 1 = 10$.

Summing all three contributions gives the coefficient of $x^4$:

$$80 - 120 + 10 = -30$$

---

## Why Ordinary Generating Functions Fail for Ordered Arrangements

Now turn to our second question. In Section 06, we counted **$4$-letter words** formed from **MISSISSIPPI** by splitting into five separate cases. Can we use generating functions to count **ordered words** with repetition automatically?

Test an **Ordinary Generating Function (OGF)** on a tiny example first so we see exactly what goes wrong.

**Test Problem:** Using the multiset $\{2 \cdot \text{A},\; 1 \cdot \text{B}\}$, how many distinct **$2$-letter words** can we form?

By direct listing, there are **$3$** distinct $2$-letter words: `AA`, `AB`, and `BA`.

Now see what happens if we naively try an OGF from Section 11:

- Bracket for `A` (at most $2$ copies): $(1 + x + x^2)$.
- Bracket for `B` (at most $1$ copy): $(1 + x)$.

Multiplying these brackets gives:

$$(1 + x + x^2)(1 + x) = 1 + 2x + 2x^2 + x^3$$

The coefficient of $x^2$ is **$2$**, *not* $3$! Why did the OGF undercount? Look at the two raw picks that produced $x^2$:

- $x^2 \cdot x^0$ (two `A`'s, zero `B`'s) gave weight **$1$**, which is correct because $\{2 \cdot \text{A}\}$ forms only $\frac{2!}{2!\,0!} = 1$ word (`AA`).
- $x^1 \cdot x^1$ (one `A`, one `B`) ALSO gave weight **$1$**, even though $\{1 \cdot \text{A},\; 1 \cdot \text{B}\}$ forms $\frac{2!}{1!\,1!} = 2$ distinct words (`AB` and `BA`)!

**The Root Cause:** An OGF counts every allowed frequency tuple $(a_1, a_2, \dots, a_r)$ summing to $n$ with weight **$1$**—it counts *unordered selections*. To count **ordered words**, each tuple $(a_1, a_2, \dots, a_r)$ summing to $n$ must instead be weighted by the **multinomial coefficient** $\frac{n!}{a_1!\,a_2!\cdots a_r!}$!

---

## Building Exponential Generating Functions

How can we make bracket multiplication automatically attach the weight $\frac{n!}{a_1!\,a_2!\cdots a_r!}$ to every tuple $(a_1, a_2, \dots, a_r)$?

Look at the structure of the multinomial coefficient:

$$\frac{n!}{a_1!\,a_2!\cdots a_r!} = n! \cdot \left(\frac{1}{a_1!} \cdot \frac{1}{a_2!} \cdots \frac{1}{a_r!}\right)$$

This factorization is the entire secret! The numerator $n!$ depends **only on the final word length $n$**, while the denominator splits cleanly into **one independent factor $\frac{1}{a_i!}$ for each letter type**!

Therefore, inside each letter's bracket, instead of writing $x^k$ when that letter is used $k$ times, we write:

$$\frac{x^k}{k!}$$

Watch what happens when we pick $\frac{x^{a_1}}{a_1!}$ from Bracket $1$, $\frac{x^{a_2}}{a_2!}$ from Bracket $2$, $\dots$, and $\frac{x^{a_r}}{a_r!}$ from Bracket $r$, and multiply them:

$$\frac{x^{a_1}}{a_1!} \cdot \frac{x^{a_2}}{a_2!} \cdots \frac{x^{a_r}}{a_r!} = \frac{1}{a_1!\,a_2!\cdots a_r!}\,x^{a_1 + a_2 + \dots + a_r}$$

When $a_1 + a_2 + \dots + a_r = n$, this raw product contributes $\frac{1}{a_1!\,a_2!\cdots a_r!}$ to the coefficient of $x^n$. Now multiply the final coefficient $[x^n]$ by **$n!$**:

$$n! \cdot [x^n]\,E(x) = \sum_{\substack{a_1 + a_2 + \dots + a_r = n \\ a_i \text{ allowed}}} \frac{n!}{a_1!\,a_2!\cdots a_r!}$$

Every allowed frequency tuple $(a_1, \dots, a_r)$ is now weighted by **its exact number of ordered arrangements**!

### Testing the Fix on Our Small Multiset

Return to $\{2 \cdot \text{A},\; 1 \cdot \text{B}\}$ using our new $\frac{x^k}{k!}$ brackets:

- Bracket for `A` ($0, 1,$ or $2$ copies): $\left(1 + \frac{x}{1!} + \frac{x^2}{2!}\right)$.
- Bracket for `B` ($0$ or $1$ copy): $\left(1 + \frac{x}{1!}\right)$.

Multiply them out carefully:

$$\left(1 + x + \frac{x^2}{2}\right)(1 + x) = 1 + 2x + \frac{3}{2}x^2 + \frac{1}{2}x^3$$

Rewrite every term in the form $a_n \cdot \frac{x^n}{n!}$ (equivalently, multiply each coefficient of $x^n$ by $n!$):

$$1 + 2\cdot\frac{x}{1!} + 3\cdot\frac{x^2}{2!} + 3\cdot\frac{x^3}{3!}$$

Check **every single length $n$** against direct counting:

- **$n = 0$:** $0! \cdot 1 = 1$ (the empty word).
- **$n = 1$:** $1! \cdot 2 = 2$ (`A`, `B`).
- **$n = 2$:** $2! \cdot \frac{3}{2} = 3$ (`AA`, `AB`, `BA`).
- **$n = 3$:** $3! \cdot \frac{1}{2} = 3$ (`AAB`, `ABA`, `BAA`).

Every single word count is exact!

![Comparing OGF and EGF Bracket Multiplication on the Multiset {2A, 1B}](https://matherama.com/content-assets/tutorials/chapter-27-combinatorics/egf_vs_ogf_mechanism.png)

### Formal Definition and Notation

**Definition (Exponential Generating Function):** Given a sequence $(a_0, a_1, a_2, \dots)$, its **Exponential Generating Function (EGF)** is the formal power series:

$$E(x) = a_0 + a_1 \frac{x}{1!} + a_2 \frac{x^2}{2!} + a_3 \frac{x^3}{3!} + \dots = \sum_{n=0}^{\infty} a_n \frac{x^n}{n!}$$

We write $\left[\frac{x^n}{n!}\right] E(x)$ for the coefficient of $\frac{x^n}{n!}$ in $E(x)$. Since the coefficient of $x^n$ is $\frac{a_n}{n!}$, we always extract $a_n$ by:

$$a_n = \left[\frac{x^n}{n!}\right] E(x) = n!\,[x^n]\,E(x)$$

> **Natural Bound vs. Infinity Still Applies!**  
> Just as in Section 11, because all exponents are non-negative, adding higher terms $\frac{x^{n+1}}{(n+1)!} + \frac{x^{n+2}}{(n+2)!} + \dots$ to any bracket **never changes** $[x^n]$. Therefore, whenever a letter has **no upper bound** (or an upper bound $b_i \ge n$, which is redundant for length $n$), we extend its bracket all the way to $\infty$!

---

## Standard Exponential Series Factors

Why are these called *exponential* generating functions? Because when an unrestricted bracket extends to $\infty$, it becomes the famous series expansion of $e^x$!

Recall the standard exponential series:

$$e^x = 1 + \frac{x}{1!} + \frac{x^2}{2!} + \frac{x^3}{3!} + \frac{x^4}{4!} + \dots = \sum_{n=0}^{\infty} \frac{x^n}{n!}$$

Replacing $x$ by $cx$ (for any constant $c$) gives the **master extraction formula** for EGFs:

$$e^{cx} = \sum_{n=0}^{\infty} \frac{(cx)^n}{n!} = \sum_{n=0}^{\infty} c^n \frac{x^n}{n!} \implies \left[\frac{x^n}{n!}\right] e^{cx} = c^n$$

*(For $c = 0$, $e^{0 \cdot x} = 1$, so $\left[\frac{x^n}{n!}\right] 1 = 0$ for every $n \ge 1$ and $1$ for $n = 0$.)*

As an instant check, if $r$ distinct letters are available in unlimited supply, each bracket is $e^x$, so their product is $(e^x)^r = e^{rx}$, and $\left[\frac{x^n}{n!}\right] e^{rx} = r^n$—immediately recovering Section 06's formula for $n$-permutations with unlimited repetition!

Now derive every standard restricted EGF factor directly from $e^x$:

**1. Unlimited supply ($k \ge 0$):**

$$1 + \frac{x}{1!} + \frac{x^2}{2!} + \frac{x^3}{3!} + \dots = e^x$$

**2. Appears at least once ($k \ge 1$):** Remove the $k = 0$ term ($1$) from $e^x$:

$$\frac{x}{1!} + \frac{x^2}{2!} + \frac{x^3}{3!} + \dots = e^x - 1$$

**3. Appears at least $a$ times ($k \ge a$):** Remove the first $a$ terms ($k = 0, 1, \dots, a - 1$) from $e^x$:

$$\frac{x^a}{a!} + \frac{x^{a+1}}{(a+1)!} + \dots = e^x - \left(1 + \frac{x}{1!} + \frac{x^2}{2!} + \dots + \frac{x^{a - 1}}{(a - 1)!}\right)$$

**4. Appears at most $b$ times ($0 \le k \le b$):** If $b < n$ (active bound), stop the bracket at $\frac{x^b}{b!}$:

$$1 + \frac{x}{1!} + \frac{x^2}{2!} + \dots + \frac{x^b}{b!}$$

*(If $b \ge n$, the cap is redundant and may be replaced cleanly by $e^x$!)*

**5. Even or odd number of occurrences:** Write $e^x$ and $e^{-x}$ side by side:

$$e^x = 1 + \frac{x}{1!} + \frac{x^2}{2!} + \frac{x^3}{3!} + \frac{x^4}{4!} + \dots$$

$$e^{-x} = 1 - \frac{x}{1!} + \frac{x^2}{2!} - \frac{x^3}{3!} + \frac{x^4}{4!} - \dots$$

Adding the two series cancels every odd power, and dividing by $2$ leaves **only even powers ($k \in \{0, 2, 4, \dots\}$)**:

$$1 + \frac{x^2}{2!} + \frac{x^4}{4!} + \dots = \frac{e^x + e^{-x}}{2}$$

Subtracting $e^{-x}$ from $e^x$ cancels every even power, and dividing by $2$ leaves **only odd powers ($k \in \{1, 3, 5, \dots\}$)**:

$$\frac{x}{1!} + \frac{x^3}{3!} + \frac{x^5}{5!} + \dots = \frac{e^x - e^{-x}}{2}$$

---

## Worked Examples and Unifying Connections

### 1. Partial Permutations of a Multiset (Fulfilling Section 06)

**Example 3 (Four-Letter Words from MISSISSIPPI):** Re-solve Example 6 of Section 06 using an Exponential Generating Function: how many distinct $4$-letter words can be formed from the letters of **MISSISSIPPI**?

**Solution:** The available multiset is $\{1 \cdot \text{M},\; 4 \cdot \text{I},\; 4 \cdot \text{S},\; 2 \cdot \text{P}\}$, and our target word length is $n = 4$. Write one EGF bracket per letter type:

- `M` (at most $1$, active since $1 < 4$): $(1 + x)$.
- `P` (at most $2$, active since $2 < 4$): $\left(1 + x + \frac{x^2}{2!}\right)$.
- `I` and `S` (at most $4$ each): notice $4 \ge n = 4$! Since the upper bound $4$ is **redundant** for $4$-letter words, we can safely extend both brackets to $e^x$!

Multiplying all four brackets gives:

$$E(x) = (1 + x)\left(1 + x + \frac{x^2}{2}\right) e^x \cdot e^x = \left(1 + 2x + \frac{3}{2}x^2 + \frac{1}{2}x^3\right) e^{2x}$$

Now extract $[x^4]\,E(x)$ using $e^{2x} = \sum_{k=0}^{\infty} \frac{2^k}{k!} x^k$. Pair each term of the polynomial with the matching power from $e^{2x}$ so the exponents sum to $4$:

$$[x^4]\,E(x) = 1 \cdot \frac{2^4}{4!} + 2 \cdot \frac{2^3}{3!} + \frac{3}{2} \cdot \frac{2^2}{2!} + \frac{1}{2} \cdot \frac{2^1}{1!}$$

$$= \frac{16}{24} + \frac{16}{6} + \frac{3}{2}(2) + \frac{1}{2}(2) = \frac{2}{3} + \frac{8}{3} + 3 + 1 = \frac{10}{3} + 4 = \frac{22}{3}$$

Finally, multiply by $4! = 24$:

$$\text{Total 4-letter words} = 4! \cdot [x^4]\,E(x) = 24 \times \frac{22}{3} = 176$$

Notice how extending the redundant caps on `I` and `S` to $e^x$ replaced all five manual cases from Section 06 with a single three-line calculation!

---

### 2. Strings with Parity Constraints

**Example 4 ($n$-Digit Strings with Even and Odd Digit Counts):** How many $n$-digit strings ($n \ge 1$, leading zeros allowed) can be formed from the digits $\{0, 1, 2, 3\}$ such that:

**(a)** the digit `0` appears an **even** number of times?

**(b)** the digit `0` appears an **even** number of times **and** the digit `1` appears an **odd** number of times?

**Solution:**

**(a)** Each of the $4$ digits has an unlimited supply, except `0` must occur an even number of times:

- Digit `0` (even): $\frac{e^x + e^{-x}}{2}$.
- Digits `1`, `2`, `3` (unrestricted): $e^x$ each.

Multiply the four brackets:

$$E(x) = \left(\frac{e^x + e^{-x}}{2}\right) e^x \cdot e^x \cdot e^x = \frac{e^{4x} + e^{2x}}{2}$$

Extract $\left[\frac{x^n}{n!}\right]$ term by term using $\left[\frac{x^n}{n!}\right] e^{cx} = c^n$:

$$a_n = \frac{4^n + 2^n}{2}$$

*(Check $n = 1$: $\frac{4 + 2}{2} = 3$, namely `1, 2, 3`. Check $n = 2$: $\frac{16 + 4}{2} = 10$, namely `00` plus all $3^2 = 9$ strings with no `0`.)*

**(b)** Now digit `0` is even, digit `1` is odd, and digits `2` and `3` are unrestricted:

$$E(x) = \left(\frac{e^x + e^{-x}}{2}\right)\left(\frac{e^x - e^{-x}}{2}\right) e^x \cdot e^x = \frac{e^{2x} - e^{-2x}}{4} \cdot e^{2x} = \frac{e^{4x} - 1}{4}$$

For every $n \ge 1$, the constant term $-1$ contributes $0$ to $\left[\frac{x^n}{n!}\right]$, leaving:

$$a_n = \frac{4^n}{4} = 4^{n - 1}$$

*(Check $n = 1$: $4^0 = 1$, namely `1`. Check $n = 2$: $4^1 = 4$, namely `12, 21, 13, 31`!)*

---

### 3. Distinct Balls into Distinct Boxes (Unifying Sections 09 and 10)

Why do **distinct-ball-into-distinct-box** distribution problems use Exponential Generating Functions?

Think carefully about what a distribution of $n$ distinct balls (`Ball 1`, `Ball 2`, $\dots$, `Ball n`) into $r$ distinct boxes (`Box 1`, `Box 2`, $\dots$, `Box r`) really is! Writing down which box receives `Ball 1`, which receives `Ball 2`, $\dots$, and which receives `Ball n` creates an **$n$-letter ordered word** over the $r$-letter alphabet $\{\text{Box 1}, \dots, \text{Box } r\}$!

- `Box i` receives $a_i$ balls $\iff$ the symbol `Box i` appears $a_i$ times in the $n$-letter word.
- Therefore, distributing **$n$ distinct balls into $r$ distinct boxes** with box-size restrictions is **100% identical** to forming **$n$-letter words** with letter-frequency restrictions!

**Example 5 (Onto Functions and Non-Empty Boxes — Recovering Sections 09 and 10):** Use an EGF to prove that the number of ways to distribute $n$ distinct balls into $r$ distinct boxes so that **no box is empty** (equivalently, the number of surjective/onto functions $r!\,S(n, r)$) is:

$$\sum_{j=0}^{r} (-1)^j \binom{r}{j} (r - j)^n$$

**Proof:** Each of the $r$ distinct boxes must receive **at least $1$ ball**, so each box contributes the EGF factor $(e^x - 1)$. Multiplying all $r$ brackets and expanding by the Binomial Theorem gives:

$$E(x) = (e^x - 1)^r = \sum_{j=0}^{r} (-1)^j \binom{r}{j} (e^x)^{r - j} = \sum_{j=0}^{r} (-1)^j \binom{r}{j} e^{(r - j)x}$$

Now extract $\left[\frac{x^n}{n!}\right]$ term by term using $\left[\frac{x^n}{n!}\right] e^{(r - j)x} = (r - j)^n$:

$$r!\,S(n, r) = \sum_{j=0}^{r} (-1)^j \binom{r}{j} (r - j)^n \qquad \blacksquare$$

*(Compare this three-line algebraic proof with the Principle of Inclusion–Exclusion proof in Section 09!)*

**Example 6 (Distinct Gifts with Mixed Conditions):** In how many ways can $n$ distinct gifts ($n \ge 1$) be distributed among three children—Asha, Kabir, and Meera—so that Asha receives **at least $1$ gift**, Kabir receives an **even** number of gifts, and Meera receives **any** number of gifts?

**Solution:** Since both gifts and children are distinct, use an EGF:

- Asha ($\ge 1$): $(e^x - 1)$.
- Kabir (even): $\frac{e^x + e^{-x}}{2}$.
- Meera (any): $e^x$.

Multiply the three factors:

$$E(x) = (e^x - 1)\left(\frac{e^x + e^{-x}}{2}\right) e^x = \frac{1}{2}(e^x - 1)(e^{2x} + 1) = \frac{1}{2}\left(e^{3x} - e^{2x} + e^x - 1\right)$$

For every $n \ge 1$, the constant $-1$ contributes $0$ to $\left[\frac{x^n}{n!}\right]$, giving:

$$a_n = \frac{3^n - 2^n + 1^n}{2} = \frac{3^n - 2^n + 1}{2}$$

*(Check $n = 2$: $\frac{9 - 4 + 1}{2} = 3$. Indeed, labeling each of the $2$ gifts by recipient gives `AA`, `AM`, `MA`—exactly $3$ distributions!)*

---

### 4. Bonus Connection: Derangements via EGFs (Section 08)

Recall **Property 4 of Derangements** from Section 08: partitioning all $n!$ permutations of $n$ objects by the exact number $k$ of objects in their original positions gave:

$$n! = \sum_{k=0}^{n} \binom{n}{k} D(n - k) = \sum_{k=0}^{n} \frac{n!}{k!\,(n - k)!}\,D(n - k)$$

Divide both sides by $n!$:

$$1 = \sum_{k=0}^{n} \frac{1}{k!} \cdot \frac{D(n - k)}{(n - k)!}$$

Look at the right-hand side! It is **literally the coefficient of $x^n$** in the product of $e^x = \sum_{k=0}^{\infty} \frac{x^k}{k!}$ and the EGF of derangements $E_D(x) = \sum_{m=0}^{\infty} D(m)\frac{x^m}{m!}$!

**Example 7 (Deriving the Derangement Formula via EGF):** Let $E_D(x) = \sum_{n=0}^{\infty} D(n)\frac{x^n}{n!}$. From the identity above, every coefficient $[x^n]$ of $e^x \cdot E_D(x)$ equals $1$, so:

$$e^x \cdot E_D(x) = 1 + x + x^2 + x^3 + \dots = \frac{1}{1 - x}$$

Divide both sides by $e^x$:

$$E_D(x) = \frac{e^{-x}}{1 - x} = \left(\sum_{k=0}^{\infty} \frac{(-1)^k}{k!} x^k\right)\left(1 + x + x^2 + x^3 + \dots\right)$$

To get $x^n$ from this product, pick $\frac{(-1)^k}{k!} x^k$ from the first bracket (for any $0 \le k \le n$) and the matching power $1 \cdot x^{n - k}$ from the second bracket. Thus $[x^n]\,E_D(x) = \sum_{k=0}^{n} \frac{(-1)^k}{k!}$, and multiplying by $n!$ immediately recovers the Derangement Formula from Section 08:

$$D(n) = n!\,[x^n]\,E_D(x) = n!\sum_{k=0}^{n} \frac{(-1)^k}{k!}$$

---

## Choosing Between OGF and EGF: Master Comparison

Students often ask: *"How do I immediately know whether a problem needs an OGF or an EGF?"* Look at whether **order / object distinctness** matters:

| Feature | Ordinary Generating Function (OGF) | Exponential Generating Function (EGF) |
| :--- | :--- | :--- |
| **Series form** | $\sum_{n=0}^{\infty} a_n x^n$ | $\sum_{n=0}^{\infty} a_n \dfrac{x^n}{n!}$ |
| **How to extract $a_n$** | $[x^n]\,G(x)$ | $\left[\dfrac{x^n}{n!}\right] E(x) = n!\,[x^n]\,E(x)$ |
| **Typical problems** | Unordered selections, integral equations $x_1 + \dots + x_r = n$, coin change, integer partitions | Ordered words / strings with repetition, multiset $r$-permutations |
| **Balls-and-boxes model** | **Identical** balls into **distinct** boxes | **Distinct** balls into **distinct** boxes |
| **No upper bound ($k \ge 0$)** | $\dfrac{1}{1 - x}$ | $e^x$ |
| **At least one ($k \ge 1$)** | $\dfrac{x}{1 - x}$ | $e^x - 1$ |
| **At most $b$ ($0 \le k \le b < n$)** | $1 + x + \dots + x^b = \dfrac{1 - x^{b+1}}{1 - x}$ | $1 + \dfrac{x}{1!} + \dfrac{x^2}{2!} + \dots + \dfrac{x^b}{b!}$ |
| **Even count ($0, 2, 4, \dots$)** | $\dfrac{1}{1 - x^2}$ | $\dfrac{e^x + e^{-x}}{2}$ |
| **Odd count ($1, 3, 5, \dots$)** | $\dfrac{x}{1 - x^2}$ | $\dfrac{e^x - e^{-x}}{2}$ |
