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 C1,C2,…,Cn—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 A1,A2 and three sets A1,A2,A3:
∣A1∪A2∣=∣A1∣+∣A2∣−∣A1∩A2∣
∣A1∪A2∪A3∣=∣A1∣+∣A2∣+∣A3∣−∣A1∩A2∣−∣A2∩A3∣−∣A3∩A1∣+∣A1∩A2∩A3∣

2. De Morgan's Laws:
(A∪B)′=A′∩B′,(A∩B)′=A′∪B′
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,…,100} be the universal set obtained by relaxing both conditions (∣U∣=100). Define:
- A1: integers in U divisible by 3 (condition C1).
- A2: integers in U divisible by 5 (condition C2).
Then A1′ is the set of integers not divisible by 3, A2′ is the set of integers not divisible by 5, and A1′∩A2′ is the set of integers divisible by neither 3 nor 5. Using De Morgan's Law and the union formula:
∣A1′∩A2′∣=∣(A1∪A2)′∣=∣U∣−∣A1∪A2∣=∣U∣−∣A1∣−∣A2∣+∣A1∩A2∣
Now compute each term (note that A1∩A2 consists of multiples of lcm(3,5)=15):
∣A1∣=⌊3100⌋=33,∣A2∣=⌊5100⌋=20,∣A1∩A2∣=⌊15100⌋=6
Substituting gives:
∣A1′∩A2′∣=100−33−20+6=53
Notice what happened geometrically: U splits into elements satisfying C1 only, C2 only, both C1 and C2, and neither. Subtracting ∣A1∣ and ∣A2∣ removed the 6 multiples of 15 twice, so adding ∣A1∩A2∣ back restored the exact count.
The General Principle of Inclusion-Exclusion§
Suppose a combinatorial universe U has n conditions C1,C2,…,Cn, and let Ai⊆U be the subset of elements satisfying condition Ci (1≤i≤n).
Theorem (Principle of Inclusion–Exclusion):
1. Complement Form (None of C1,C2,…,Cn hold):
∣A1′∩A2′∩⋯∩An′∣=∣U∣−∑∣Ai∣+∑∣Ai∩Aj∣−∑∣Ai∩Aj∩Ak∣+⋯+(−1)n∣A1∩A2∩⋯∩An∣
2. Union Form (At least one of C1,C2,…,Cn holds):
∣A1∪A2∪⋯∪An∣=∑∣Ai∣−∑∣Ai∩Aj∣+∑∣Ai∩Aj∩Ak∣−⋯+(−1)n−1∣A1∩A2∩⋯∩An∣
Understanding the Sigma Notation§
Make sure you know exactly what each summation represents and how many terms it contains:
- ∑∣Ai∣=∣A1∣+∣A2∣+⋯+∣An∣ — sum of all single sets ((1n) terms).
- ∑∣Ai∩Aj∣=∣A1∩A2∣+∣A1∩A3∣+⋯+∣An−1∩An∣ — sum over all pairs of distinct sets ((2n) terms).
- In general, ∑∣Ai1∩Ai2∩⋯∩Aik∣ is the sum over all possible k-fold intersections (1≤i1<i2<⋯<ik≤n), containing exactly (kn) terms.
Proof of the Complement Form: Take any element x∈U, and suppose x belongs to exact m of the sets A1,A2,…,An (0≤m≤n). Let us count how many times x is counted on both sides:
- Case m=0 (x lies in none of the Ai): On the LHS, x∈A1′∩⋯∩An′, so x contributes 1. On the RHS, x is counted once in ∣U∣ and 0 times in every intersection sum, giving 1.
- Case m≥1 (x lies in m of the sets): On the LHS, x∈/A1′∩⋯∩An′, so x contributes 0. On the RHS, x is counted once in ∣U∣, (1m) times in ∑∣Ai∣, (2m) times in ∑∣Ai∩Aj∣, ..., and (mm) 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:
1−(1m)+(2m)−(3m)+⋯+(−1)m(mm)=(1−1)m=0
Since every element x∈U contributes identically to both sides, the two sides are equal. Subtracting from ∣U∣ immediately gives the Union Form. ■
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 A1,A2,A3 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!.
- ∣A1∣=7! (tie
DROP into 1 unit +6 remaining letters =7 units).
- ∣A2∣=8! (tie
THE into 1 unit +7 remaining letters =8 units).
- ∣A3∣=8! (tie
MIC into 1 unit +7 remaining letters =8 units).
- ∣A1∩A2∣=5! (
DROP, THE, and M, I, C =5 units).
- ∣A2∩A3∣=6! (
THE, MIC, and D, R, O, P =6 units).
- ∣A1∩A3∣=5! (
DROP, MIC, and T, H, E =5 units).
- ∣A1∩A2∩A3∣=3! (the 3 units
DROP, THE, MIC).
Applying PIE:
∣A1′∩A2′∩A3′∣=∣U∣−∣A1∣−∣A2∣−∣A3∣+∣A1∩A2∣+∣A2∩A3∣+∣A1∩A3∣−∣A1∩A2∩A3∣
=10!−7!−2(8!)+6!+2(5!)−3!=3,628,800−5040−80,640+720+240−6=3,544,074
Example 3 (Three Divisors): How many integers from 1 to 1000 are divisible by none of 2, 3, or 5?
Solution: Let A1,A2,A3 be multiples of 2,3,5 in {1,…,1000}. Then:
- ∣A1∣=⌊1000/2⌋=500, ∣A2∣=⌊1000/3⌋=333, ∣A3∣=⌊1000/5⌋=200.
- ∣A1∩A2∣=⌊1000/6⌋=166, ∣A2∩A3∣=⌊1000/15⌋=66, ∣A1∩A3∣=⌊1000/10⌋=100.
- ∣A1∩A2∩A3∣=⌊1000/30⌋=33.
By PIE:
∣A1′∩A2′∩A3′∣=1000−(500+333+200)+(166+66+100)−33=1000−1033+332−33=266
Example 4 (Upper-Bounded Integral Equation — Extending Section 07): Find the number of integer solutions to x1+x2+x3=15 subject to 0≤x1≤6, 0≤x2≤7, and 0≤x3≤8.
Solution: Let U be all non-negative integer solutions of x1+x2+x3=15, so ∣U∣=(215+3−1)=(217)=136. Define the three violation sets where each upper bound fails:
- A1 (x1≥7): Shift by 7⟹ sum =8⟹∣A1∣=(28+2)=(210)=45.
- A2 (x2≥8): Shift by 8⟹ sum =7⟹∣A2∣=(27+2)=(29)=36.
- A3 (x3≥9): Shift by 9⟹ sum =6⟹∣A3∣=(26+2)=(28)=28.
Check pairwise and triple violations:
- A1∩A2 (x1≥7,x2≥8): Shift sum by 15⟹ remaining sum =0⟹∣A1∩A2∣=(20+2)=1.
- A1∩A3 (x1≥7,x3≥9): Minimum sum 16>15⟹∣A1∩A3∣=0.
- A2∩A3 (x2≥8,x3≥9): Minimum sum 17>15⟹∣A2∩A3∣=0.
- A1∩A2∩A3: Clearly 0.
By the Principle of Inclusion–Exclusion:
∣A1′∩A2′∩A3′∣=136−(45+36+28)+(1+0+0)−0=28
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 Dn).
Theorem (Derangement Formula): For every positive integer n≥1:
D(n)=n!(1−1!1+2!1−3!1+⋯+n!(−1)n)=n!k=0∑nk!(−1)k
(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∈{1,2,…,n}, let Ak 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 Ak′ mean? That the k-th person does not have their own hat. Hence A1′∩A2′∩⋯∩An′ is precisely the set of arrangements in which no one has their own hat—so D(n)=∣A1′∩A2′∩⋯∩An′∣.
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 ∣Ai∣=(n−1)!. Since there are (1n) terms: ∑∣Ai∣=(1n)(n−1)!.
- Pairs: Fixing both the i-th and j-th persons' hats leaves (n−2)! permutations of the rest. Since there are (2n) pairs: ∑∣Ai∩Aj∣=(2n)(n−2)!.
- In general (k-fold intersections): ∑∣Ai1∩⋯∩Aik∣=(kn)(n−k)!=k!n!.
Substituting into PIE:
D(n)=n!−(1n)(n−1)!+(2n)(n−2)!−(3n)(n−3)!+⋯+(−1)n(nn)(n−n)!
=n!−1!n!+2!n!−3!n!+⋯+(−1)nn!n!=n!(1−1!1+2!1−3!1+⋯+n!(−1)n)■
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−1!1+2!1−3!1+⋯≈0.3679, the fraction of permutations that are derangements rapidly approaches 1/e≈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 (kn) ways. The remaining n−k people must all receive wrong hats, which can be done in D(n−k) ways. Total ways:
(kn)D(n−k)
2. Mr. X always takes Mr. Y's hat (X=Y): Suppose among n people (n≥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:
D(n−1)+D(n−2)

3. Recurrence Relation: For every n≥2:
D(n)=(n−1)(D(n−1)+D(n−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)(D(n−1)+D(n−2)). ■
(Algebraic simplification of the explicit formula also gives the one-step recurrence D(n)=nD(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 S0,S1,…,Sn, where Sk contains all permutations with exact k objects in their original positions (∣Sk∣=(kn)D(n−k)). By the Addition Principle:
n!=(0n)D(n)+(1n)D(n−1)+(2n)D(n−2)+⋯+(nn)D(0)
5. Partial Derangement (k specific people do not get their own hats): Suppose we only require that k specific people (P1,P2,…,Pk) 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 A1,A2,…,Ak gives:
∣A1′∩A2′∩⋯∩Ak′∣=n!−(1k)(n−1)!+(2k)(n−2)!−⋯+(−1)k(kk)(n−k)!
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∈{2,3,4,5} of letters placed correctly:
(25)D(3)+(35)D(2)+(45)D(1)+(55)D(0)=10(2)+10(1)+5(0)+1(1)=31
(c) Apply Property 5 with n=5 and k=2:
5!−(12)4!+(22)3!=120−48+6=78
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 φ(n).
Definition: For any positive integer n, φ(n) is the count of positive integers m∈{1,2,…,n} that are coprime to n—that is, gcd(m,n)=1.
Checking small values directly:
- φ(1)=1 (since gcd(1,1)=1).
- φ(2)=1 (1).
- φ(3)=2 (1,2).
- φ(4)=2 (1,3).
- φ(5)=4 (1,2,3,4; in general, φ(p)=p−1 for any prime p).
Theorem (Euler's Product Formula): If n has the prime factorization n=p1m1p2m2⋯pkmk (where p1,p2,…,pk are distinct primes), then:
φ(n)=n(1−p11)(1−p21)⋯(1−pk1)=nr=1∏k(1−pr1)
Proof (Via PIE): Let U={1,2,…,n} (∣U∣=n). For each distinct prime factor pr of n (1≤r≤k), let Ar be the set of integers in U divisible by pr:
Ar={pr,2pr,3pr,…,⌊prn⌋pr}⟹∣Ar∣=⌊prn⌋=prn
Why can we drop the greatest-integer brackets ⌊⋅⌋? Because pr is a prime factor of n, so n/pr is already an exact integer! Similarly, for any two distinct primes pi,pj dividing n, their product pipj also divides n, so:
∣Ai∩Aj∣=pipjn,∣Ai∩Aj∩Al∣=pipjpln,…,∣A1∩⋯∩Ak∣=p1p2⋯pkn
An integer in U is coprime to n if and only if it shares none of the prime factors p1,p2,…,pk—which is precisely the set A1′∩A2′∩⋯∩Ak′. Applying PIE:
φ(n)=n−(p1n+p2n+⋯+pkn)+(p1p2n+p1p3n+…)−⋯+(−1)kp1p2⋯pkn
Factoring n out of every term reveals the exact expansion of the product:
φ(n)=n(1−p11)(1−p21)⋯(1−pk1)■
Example 6: Compute φ(12), φ(360), and φ(pa) for a prime p and a≥1.
Solution:
- n=12=22⋅3: φ(12)=12(1−21)(1−31)=12⋅21⋅32=4 (namely 1,5,7,11).
- n=360=23⋅32⋅5: φ(360)=360(1−21)(1−31)(1−51)=360⋅21⋅32⋅54=96.
- n=pa: Only one distinct prime factor p divides pa, so φ(pa)=pa(1−p1)=pa−pa−1 (every integer from 1 to pa except the pa−1 multiples of p).
Summary Table§
| Concept |
Formula |
Key Application |
| PIE (None of A1,…,An) |
∣U∣−∑∣Ai∣+∑∣Ai∩Aj∣−⋯+(−1)n∣⋂Ai∣ |
Counting configurations satisfying none of n forbidden conditions |
| PIE (At least one of A1,…,An) |
∑∣Ai∣−∑∣Ai∩Aj∣+⋯+(−1)n−1∣⋂Ai∣ |
Counting unions of overlapping sets |
| Derangements D(n) |
n!∑k=0nk!(−1)k |
Permutations with zero objects in their own positions |
| Derangement Recurrence |
D(n)=(n−1)(D(n−1)+D(n−2)) |
Fast recursive evaluation of D(n) |
| Exact k Fixed Points |
(kn)D(n−k) |
Partial matching problems |
| Euler's Totient φ(n) |
n∏p∣n(1−p1) |
Count of integers in {1,…,n} coprime to n |