Inclusion–Exclusion and Pigeonhole
Counting unions of overlapping sets, derangements, and the pigeonhole principle as a proof device.
Assumes you know
Inclusion–Exclusion and Pigeonhole
Intuition first
Two counting principles that look almost trivial and repeatedly settle problems that resist direct attack.
Inclusion–exclusion fixes double counting. If 30 students take maths and 25 take physics, the number taking at least one is not 55 unless nobody takes both. Subtract the overlap. With three sets you subtract the pairwise overlaps and then have to add back the triple overlap, because subtracting the pairs removed it too many times. The alternating pattern continues for any number of sets.
Pigeonhole is even simpler: put 13 people in a room and two share a birth month, because there are only 12 months. No cleverness, just arithmetic — and it proves existence results that would otherwise be hard, precisely because it needs no construction.
Inclusion–exclusion
Two sets:
Three sets:
In general, for sets:
Why the alternating signs are exactly rightAdvanced
Take an element belonging to exactly of the sets, . It should be counted once in the union. Count how many times the formula counts it.
It appears in single sets, pairwise intersections, triples, and so on. With the alternating signs its total contribution is
Now recall the binomial expansion of :
Since , rearranging gives
So every element in at least one set is counted exactly once, regardless of . Elements in no set contribute nothing. The formula is exact.
The probability version is identical with replaced by .
Solved problem 1 · Three languages
Of 100 students: 45 study French, 38 German, 30 Spanish; 18 study French and German, 12 French and Spanish, 10 German and Spanish, and 5 study all three. How many study none?
Step 1 — apply the three-set formula
Step 2 — evaluate in stages
Step 3 — take the complement
Step 4 — verify with a region-by-region breakdown
Compute each disjoint region and check the total.
All three: 5.
Exactly two:
Exactly one:
Sum:
Answer
students study at least one language, so study none.
Derangements
A derangement is a permutation with no fixed point — nothing in its original position. Let be the number of derangements of items.
Counting derangements by inclusion–exclusionAdvanced
Let be the set of permutations fixing item . We want permutations in none of the , which is the complement of their union.
Fixing a specific set of items leaves the other free, so any such intersection has permutations, and there are ways to choose which are fixed:
Therefore
Expand :
The sum is the truncated series for , so
The probability that a random permutation has no fixed point converges to — and does so fast: it is already accurate to four decimal places at . Notably, this limit does not depend on , which pairs with the earlier result that the expected number of fixed points is exactly 1 for every .
Solved problem 2 · The hat-check problem
Five people leave hats at a cloakroom and receive them back at random. What is the probability nobody gets their own hat?
Step 1 — total outcomes
Step 2 — count derangements using the formula
Evaluate the bracket:
Step 3 — the probability
Step 4 — compare with the limit
At the exact answer is already within of the limit.
Answer
, very close to .
The pigeonhole principle
Basic form. If items go into containers and , some container holds at least two items.
Generalised form. With items in containers, some container holds at least items.
Solved problem 3 · Two applications
(a) In any group of 13 people, two share a birth month. (b) In any set of 5 points inside a unit square, two are within distance .
(a) — identify items and containers
Items: 13 people. Containers: 12 months. Since , some month holds at least two people.
The generalised form gives , the same conclusion.
(b) — construct the containers
Divide the unit square into four sub-squares of side by cutting at the midpoints.
With 5 points in 4 sub-squares, some sub-square contains at least points.
(b) — bound the distance within a sub-square
The largest distance inside a square of side is its diagonal, . Here :
So those two points are at most apart.
Answer
Both follow immediately once the containers are chosen well. Choosing the containers is the creative step — the principle itself is arithmetic.
from math import comb, factorial, exp, ceil
# Three languages.
union = 45 + 38 + 30 - 18 - 12 - 10 + 5
print(f"at least one: {union} none: {100 - union}")
# Derangements, two ways.
def derangements(n):
return sum((-1)**j * comb(n, j) * factorial(n - j) for j in range(n + 1))
print(f"\n{'n':>3} {'D_n':>10} {'D_n/n!':>10} {'1/e':>10}")
for n in range(1, 11):
d = derangements(n)
print(f"{n:3d} {d:10d} {d/factorial(n):10.6f} {exp(-1):10.6f}")
# Pigeonhole: guaranteed minimum occupancy.
for items, containers in ((13, 12), (5, 4), (100, 7)):
print(f"\n{items} items in {containers} containers "
f"-> some container has >= {ceil(items/containers)}")Exercise 1
How many integers from 1 to 100 are divisible by 2, 3 or 5?
Show solutionHide solution
Let be the multiples of 2, 3 and 5. Counts use floor division:
Pairwise intersections are multiples of the products:
Triple intersection, multiples of 30:
Apply the formula:
74 integers. So 26 are coprime to 30 — which matches Euler's totient reasoning: , close since 100 is not an exact multiple of 30.
Exercise 2
Show that among any 5 integers, some pair has a difference divisible by 4.
Show solutionHide solution
Consider each integer's remainder on division by 4. There are exactly four possible remainders: — these are the containers.
With 5 integers and 4 remainder classes, pigeonhole guarantees two integers and share a remainder :
Their difference is
which is divisible by 4.
The argument generalises directly: among any integers, some pair has a difference divisible by . Note again that it identifies no particular pair — only that one exists.