Principle of Inclusion-Exclusion, Derangements, and Euler Totient Function
In Section 01, the Subtraction Principle handled problems with a single restriction: \(|A'| = |U| - |A|\). But what if a counting problem imposes two, three, or \(n\) simultaneous restrictions \(C_1, C_2, \dots, C_n\)—and we want to count configurations that satisfy none of them (or at least one of them)?
Subtracting each restricted set separately over-subtracts the elements that belong to two or more sets at once. The Principle of Inclusion–Exclusion (PIE) is the exact generalization of the Subtraction Principle that corrects every overlap systematically.
Set-Theoretic Foundation and Introductory Example
Recall three foundational laws from Set Theory (Chapter 01):
1. Inclusion–Exclusion Law for Unions: For two sets \(A_1, A_2\) and three sets \(A_1, A_2, A_3\):
2. De Morgan's Laws:
3. Complement Law: For any subset \(A\) of a universal set \(U\), \(|A'| = |U| - |A|\).
Before stating the general \(n\)-set formula, watch how these laws work together on a concrete problem.
Example 1: Find the count of integers from \(1\) to \(100\) that are divisible by neither \(3\) nor \(5\).
Solution: Let \(U = \{1, 2, \dots, 100\}\) be the universal set obtained by relaxing both conditions (\(|U| = 100\)). Define:
- \(A_1\): integers in \(U\) divisible by \(3\) (condition \(C_1\)).
- \(A_2\): integers in \(U\) divisible by \(5\) (condition \(C_2\)).
Then \(A_1'\) is the set of integers not divisible by \(3\), \(A_2'\) is the set of integers not divisible by \(5\), and \(A_1' \cap A_2'\) is the set of integers divisible by neither \(3\) nor \(5\). Using De Morgan's Law and the union formula:
Now compute each term (note that \(A_1 \cap A_2\) consists of multiples of \(\operatorname{lcm}(3, 5) = 15\)):
Substituting gives:
Notice what happened geometrically: \(U\) splits into elements satisfying \(C_1\) only, \(C_2\) only, both \(C_1\) and \(C_2\), and neither. Subtracting \(|A_1|\) and \(|A_2|\) removed the \(6\) multiples of \(15\) twice, so adding \(|A_1 \cap A_2|\) back restored the exact count.
The General Principle of Inclusion-Exclusion
Suppose a combinatorial universe \(U\) has \(n\) conditions \(C_1, C_2, \dots, C_n\), and let \(A_i \subseteq U\) be the subset of elements satisfying condition \(C_i\) (\(1 \le i \le n\)).
Theorem (Principle of Inclusion–Exclusion):
1. Complement Form (None of \(C_1, C_2, \dots, C_n\) hold):
2. Union Form (At least one of \(C_1, C_2, \dots, C_n\) holds):
Understanding the Sigma Notation
Make sure you know exactly what each summation represents and how many terms it contains:
- \(\sum |A_i| = |A_1| + |A_2| + \dots + |A_n|\) — sum of all single sets (\(\binom{n}{1}\) terms).
- \(\sum |A_i \cap A_j| = |A_1 \cap A_2| + |A_1 \cap A_3| + \dots + |A_{n-1} \cap A_n|\) — sum over all pairs of distinct sets (\(\binom{n}{2}\) terms).
- In general, \(\sum |A_{i_1} \cap A_{i_2} \cap \dots \cap A_{i_k}|\) is the sum over all possible \(k\)-fold intersections (\(1 \le i_1 < i_2 < \dots < i_k \le n\)), containing exactly \(\binom{n}{k}\) terms.
Proof of the Complement Form: Take any element \(x \in U\), and suppose \(x\) belongs to exact \(m\) of the sets \(A_1, A_2, \dots, A_n\) (\(0 \le m \le n\)). Let us count how many times \(x\) is counted on both sides:
- Case \(m = 0\) (\(x\) lies in none of the \(A_i\)): On the LHS, \(x \in A_1' \cap \dots \cap A_n'\), so \(x\) contributes \(1\). On the RHS, \(x\) is counted once in \(|U|\) and \(0\) times in every intersection sum, giving \(1\).
- Case \(m \ge 1\) (\(x\) lies in \(m\) of the sets): On the LHS, \(x \notin A_1' \cap \dots \cap A_n'\), so \(x\) contributes \(0\). On the RHS, \(x\) is counted once in \(|U|\), \(\binom{m}{1}\) times in \(\sum |A_i|\), \(\binom{m}{2}\) times in \(\sum |A_i \cap A_j|\), ..., and \(\binom{m}{m}\) times in the \(m\)-fold intersection sum (and \(0\) times in higher intersections). By the Binomial Theorem for \((1 + x)^m\) evaluated at \(x = -1\), its net contribution on the RHS is:
Since every element \(x \in U\) contributes identically to both sides, the two sides are equal. Subtracting from \(|U|\) immediately gives the Union Form. \(\blacksquare\)
Example 2 (Forbidden Substrings — DROPTHEMIC): Find the number of arrangements of the \(10\)-letter word DROPTHEMIC (all \(10\) letters distinct) in which none of the blocks DROP, THE, or MIC appear as contiguous substrings.
Solution: Let \(U\) be all \(10!\) unrestricted arrangements. Define \(A_1, A_2, A_3\) as the sets of arrangements containing DROP, THE, and MIC respectively. Because these three blocks share no common letters, we simply bundle each required block as a single unit:
- \(|U| = 10!\).
- \(|A_1| = 7!\) (tie
DROPinto \(1\) unit \(+ 6\) remaining letters \(= 7\) units). - \(|A_2| = 8!\) (tie
THEinto \(1\) unit \(+ 7\) remaining letters \(= 8\) units). - \(|A_3| = 8!\) (tie
MICinto \(1\) unit \(+ 7\) remaining letters \(= 8\) units). - \(|A_1 \cap A_2| = 5!\) (
DROP,THE, andM, I, C\(= 5\) units). - \(|A_2 \cap A_3| = 6!\) (
THE,MIC, andD, R, O, P\(= 6\) units). - \(|A_1 \cap A_3| = 5!\) (
DROP,MIC, andT, H, E\(= 5\) units). - \(|A_1 \cap A_2 \cap A_3| = 3!\) (the \(3\) units
DROP,THE,MIC).
Applying PIE:
Example 3 (Three Divisors): How many integers from \(1\) to \(1000\) are divisible by none of \(2\), \(3\), or \(5\)?
Solution: Let \(A_1, A_2, A_3\) be multiples of \(2, 3, 5\) in \(\{1, \dots, 1000\}\). Then:
- \(|A_1| = \lfloor 1000/2 \rfloor = 500\), \(|A_2| = \lfloor 1000/3 \rfloor = 333\), \(|A_3| = \lfloor 1000/5 \rfloor = 200\).
- \(|A_1 \cap A_2| = \lfloor 1000/6 \rfloor = 166\), \(|A_2 \cap A_3| = \lfloor 1000/15 \rfloor = 66\), \(|A_1 \cap A_3| = \lfloor 1000/10 \rfloor = 100\).
- \(|A_1 \cap A_2 \cap A_3| = \lfloor 1000/30 \rfloor = 33\).
By PIE:
Example 4 (Upper-Bounded Integral Equation — Extending Section 07): Find the number of integer solutions to \(x_1 + x_2 + x_3 = 15\) subject to \(0 \le x_1 \le 6\), \(0 \le x_2 \le 7\), and \(0 \le x_3 \le 8\).
Solution: Let \(U\) be all non-negative integer solutions of \(x_1 + x_2 + x_3 = 15\), so \(|U| = \binom{15 + 3 - 1}{2} = \binom{17}{2} = 136\). Define the three violation sets where each upper bound fails:
- \(A_1\) (\(x_1 \ge 7\)): Shift by \(7 \implies\) sum \(= 8 \implies |A_1| = \binom{8 + 2}{2} = \binom{10}{2} = 45\).
- \(A_2\) (\(x_2 \ge 8\)): Shift by \(8 \implies\) sum \(= 7 \implies |A_2| = \binom{7 + 2}{2} = \binom{9}{2} = 36\).
- \(A_3\) (\(x_3 \ge 9\)): Shift by \(9 \implies\) sum \(= 6 \implies |A_3| = \binom{6 + 2}{2} = \binom{8}{2} = 28\).
Check pairwise and triple violations:
- \(A_1 \cap A_2\) (\(x_1 \ge 7,\; x_2 \ge 8\)): Shift sum by \(15 \implies\) remaining sum \(= 0 \implies |A_1 \cap A_2| = \binom{0 + 2}{2} = 1\).
- \(A_1 \cap A_3\) (\(x_1 \ge 7,\; x_3 \ge 9\)): Minimum sum \(16 > 15 \implies |A_1 \cap A_3| = 0\).
- \(A_2 \cap A_3\) (\(x_2 \ge 8,\; x_3 \ge 9\)): Minimum sum \(17 > 15 \implies |A_2 \cap A_3| = 0\).
- \(A_1 \cap A_2 \cap A_3\): Clearly \(0\).
By the Principle of Inclusion–Exclusion:
Derangements
Imagine \(n\) people sitting in a room, each wearing their own hat. They toss all \(n\) hats into a pile and redistribute them so that no one receives their own hat—every person ends up wearing someone else's hat. In how many ways can this happen?
A permutation of \(n\) distinct objects in which no object sits in its original designated position is called a derangement, and the total number of derangements of \(n\) objects is denoted by \(D(n)\) (or \(D_n\)).
Theorem (Derangement Formula): For every positive integer \(n \ge 1\):
(with the convention \(D(0) = 1\), representing the single empty arrangement of \(0\) hats).
Proof (Via PIE): Let \(U\) be all \(n!\) permutations of the \(n\) hats (\(|U| = n!\)). For each \(k \in \{1, 2, \dots, n\}\), let \(A_k\) be the set of permutations in which the \(k\)-th person receives their own hat (we do not restrict what happens to the other \(n - 1\) people).
What does \(A_k'\) mean? That the \(k\)-th person does not have their own hat. Hence \(A_1' \cap A_2' \cap \dots \cap A_n'\) is precisely the set of arrangements in which no one has their own hat—so \(D(n) = |A_1' \cap A_2' \cap \dots \cap A_n'|\).
Compute each intersection sum:
- Single sets: Fixing the \(i\)-th person's hat leaves the remaining \(n - 1\) hats to permute in \((n - 1)!\) ways, so \(|A_i| = (n - 1)!\). Since there are \(\binom{n}{1}\) terms: \(\sum |A_i| = \binom{n}{1}(n - 1)!\).
- Pairs: Fixing both the \(i\)-th and \(j\)-th persons' hats leaves \((n - 2)!\) permutations of the rest. Since there are \(\binom{n}{2}\) pairs: \(\sum |A_i \cap A_j| = \binom{n}{2}(n - 2)!\).
- In general (\(k\)-fold intersections): \(\sum |A_{i_1} \cap \dots \cap A_{i_k}| = \binom{n}{k}(n - k)! = \frac{n!}{k!}\).
Substituting into PIE:
Computing the first several values of \(D(n)\) is worth memorizing:
| \(n\) | \(0\) | \(1\) | \(2\) | \(3\) | \(4\) | \(5\) | \(6\) |
|---|---|---|---|---|---|---|---|
| \(D(n)\) | \(1\) | \(0\) | \(1\) | \(2\) | \(9\) | \(44\) | \(265\) |
(Remarkable connection to calculus: since \(e^{-1} = 1 - \frac{1}{1!} + \frac{1}{2!} - \frac{1}{3!} + \dots \approx 0.3679\), the fraction of permutations that are derangements rapidly approaches \(1 / e \approx 36.8\%\) as \(n\) grows!)
Five Essential Properties of Derangements
Let \(n\) people each own a hat, and consider the following variations:
1. Exactly \(k\) people receive their own hats: First choose which \(k\) people get their own hats in \(\binom{n}{k}\) ways. The remaining \(n - k\) people must all receive wrong hats, which can be done in \(D(n - k)\) ways. Total ways:
2. Mr. \(X\) always takes Mr. \(Y\)'s hat (\(X \neq Y\)): Suppose among \(n\) people (\(n \ge 2\)), Mr. \(X\) insists on keeping Mr. \(Y\)'s hat, and all \(n\) people must end up with wrong hats. Is the answer simply \(D(n - 1)\)? No! Let us see why.
Picture Mr. \(X\) walking out of the room wearing Mr. \(Y\)'s hat. Now \(n - 1\) people remain in the room along with \(n - 1\) hats (everyone's own hat except Mr. \(Y\)'s—and Mr. \(X\)'s abandoned hat sitting on the table). Mr. \(Y\) reluctantly picks up Mr. \(X\)'s abandoned hat and faces two disjoint choices:
- Case I (Mr. \(Y\) keeps Mr. \(X\)'s hat and steps out of the swap): Both \(X\) and \(Y\) now have wrong hats! The remaining \(n - 2\) people simply derange their own \(n - 2\) hats in \(D(n - 2)\) ways.
- Case II (Mr. \(Y\) refuses to keep Mr. \(X\)'s hat and joins the derangement): Treating Mr. \(X\)'s hat temporarily as "Mr. \(Y\)'s forbidden hat," every one of the \(n - 1\) remaining people now has exactly one forbidden hat—which is precisely a derangement of \(n - 1\) people in \(D(n - 1)\) ways.
Adding these two disjoint cases gives the total number of valid derangements:
3. Recurrence Relation: For every \(n \ge 2\):
Proof: Person \(1\) cannot receive Hat \(1\), so Person \(1\) has \(n - 1\) choices of whose hat to take (Hat \(2\), Hat \(3\), ..., or Hat \(n\)). By Property 2, for each of those \(n - 1\) choices, the remaining \(n - 1\) hats can be deranged in \(D(n - 1) + D(n - 2)\) ways. Multiplying yields \(D(n) = (n - 1)\big(D(n - 1) + D(n - 2)\big)\). \(\blacksquare\)
(Algebraic simplification of the explicit formula also gives the one-step recurrence \(D(n) = n\,D(n - 1) + (-1)^n\).)
4. Partitioning All \(n!\) Permutations by Fixed Points: Let \(S\) be all \(n!\) permutations of \(n\) objects, and partition \(S\) into \(S_0, S_1, \dots, S_n\), where \(S_k\) contains all permutations with exact \(k\) objects in their original positions (\(|S_k| = \binom{n}{k} D(n - k)\)). By the Addition Principle:
5. Partial Derangement (\(k\) specific people do not get their own hats): Suppose we only require that \(k\) specific people (\(P_1, P_2, \dots, P_k\)) do not receive their own hats, while we do not care whether the remaining \(n - k\) people get their own hats or not. Applying PIE solely to the \(k\) sets \(A_1, A_2, \dots, A_k\) gives:
Example 5: Five letters are addressed to \(5\) different recipients. In how many ways can the letters be placed into the \(5\) envelopes so that: (a) no letter goes into its correct envelope, (b) at least \(2\) letters go into their correct envelopes, (c) neither Letter \(1\) nor Letter \(2\) goes into its correct envelope (with no restriction on Letters \(3, 4, 5\))?
Solution:
(a) \(D(5) = 44\).
(b) Partition by exact number \(k \in \{2, 3, 4, 5\}\) of letters placed correctly:
(c) Apply Property 5 with \(n = 5\) and \(k = 2\):
Euler's Totient Function
One of the most elegant applications of the Principle of Inclusion–Exclusion in number theory is Euler's Totient Function, denoted \(\varphi(n)\).
Definition: For any positive integer \(n\), \(\varphi(n)\) is the count of positive integers \(m \in \{1, 2, \dots, n\}\) that are coprime to \(n\)—that is, \(\gcd(m, n) = 1\).
Checking small values directly:
- \(\varphi(1) = 1\) (since \(\gcd(1, 1) = 1\)).
- \(\varphi(2) = 1\) (\(1\)).
- \(\varphi(3) = 2\) (\(1, 2\)).
- \(\varphi(4) = 2\) (\(1, 3\)).
- \(\varphi(5) = 4\) (\(1, 2, 3, 4\); in general, \(\varphi(p) = p - 1\) for any prime \(p\)).
Theorem (Euler's Product Formula): If \(n\) has the prime factorization \(n = p_1^{m_1} p_2^{m_2} \cdots p_k^{m_k}\) (where \(p_1, p_2, \dots, p_k\) are distinct primes), then:
Proof (Via PIE): Let \(U = \{1, 2, \dots, n\}\) (\(|U| = n\)). For each distinct prime factor \(p_r\) of \(n\) (\(1 \le r \le k\)), let \(A_r\) be the set of integers in \(U\) divisible by \(p_r\):
Why can we drop the greatest-integer brackets \(\lfloor \cdot \rfloor\)? Because \(p_r\) is a prime factor of \(n\), so \(n / p_r\) is already an exact integer! Similarly, for any two distinct primes \(p_i, p_j\) dividing \(n\), their product \(p_i p_j\) also divides \(n\), so:
An integer in \(U\) is coprime to \(n\) if and only if it shares none of the prime factors \(p_1, p_2, \dots, p_k\)—which is precisely the set \(A_1' \cap A_2' \cap \dots \cap A_k'\). Applying PIE:
Factoring \(n\) out of every term reveals the exact expansion of the product:
Example 6: Compute \(\varphi(12)\), \(\varphi(360)\), and \(\varphi(p^a)\) for a prime \(p\) and \(a \ge 1\).
Solution:
- \(n = 12 = 2^2 \cdot 3\): \(\varphi(12) = 12\left(1 - \frac{1}{2}\right)\left(1 - \frac{1}{3}\right) = 12 \cdot \frac{1}{2} \cdot \frac{2}{3} = 4\) (namely \(1, 5, 7, 11\)).
- \(n = 360 = 2^3 \cdot 3^2 \cdot 5\): \(\varphi(360) = 360\left(1 - \frac{1}{2}\right)\left(1 - \frac{1}{3}\right)\left(1 - \frac{1}{5}\right) = 360 \cdot \frac{1}{2} \cdot \frac{2}{3} \cdot \frac{4}{5} = 96\).
- \(n = p^a\): Only one distinct prime factor \(p\) divides \(p^a\), so \(\varphi(p^a) = p^a\left(1 - \frac{1}{p}\right) = p^a - p^{a - 1}\) (every integer from \(1\) to \(p^a\) except the \(p^{a - 1}\) multiples of \(p\)).
Summary Table
| Concept | Formula | Key Application |
|---|---|---|
| PIE (None of \(A_1, \dots, A_n\)) | \(\lvert U \rvert - \sum \lvert A_i \rvert + \sum \lvert A_i \cap A_j \rvert - \dots + (-1)^n \lvert \bigcap A_i \rvert\) | Counting configurations satisfying none of \(n\) forbidden conditions |
| PIE (At least one of \(A_1, \dots, A_n\)) | \(\sum \lvert A_i \rvert - \sum \lvert A_i \cap A_j \rvert + \dots + (-1)^{n-1} \lvert \bigcap A_i \rvert\) | Counting unions of overlapping sets |
| Derangements \(D(n)\) | \(n! \sum_{k=0}^n \frac{(-1)^k}{k!}\) | Permutations with zero objects in their own positions |
| Derangement Recurrence | \(D(n) = (n - 1)\big(D(n - 1) + D(n - 2)\big)\) | Fast recursive evaluation of \(D(n)\) |
| Exact \(k\) Fixed Points | \(\binom{n}{k} D(n - k)\) | Partial matching problems |
| Euler's Totient \(\varphi(n)\) | \(n \prod_{p \mid n} \left(1 - \frac{1}{p}\right)\) | Count of integers in \(\{1, \dots, n\}\) coprime to \(n\) |

