Folioby Interconnected
Log InSign Up

Inclusion--exclusion: why the alternating signs work

The inclusion--exclusion principle corrects for overcounting by alternately adding and subtracting intersection sizes. We prove the general formula using indicator functions, apply it to count derangements, and trace the thread forward to the Mobius inversion formula on partially ordered sets.

FO
Folio Official
March 1, 2026

The inclusion–exclusion principle instructs us to add, subtract, add again, subtract again – an operation that seems almost whimsical. For two sets and a Venn diagram the logic is intuitive, but as the number of sets grows to three, four, or more, why does this alternating pattern of signs continue to yield the correct count? Where does the cancellation come from?

1 Two sets: the Venn diagram

Theorem 1 (Inclusion–exclusion for two sets).
For finite sets A and B,
∣A∪B∣=∣A∣+∣B∣−∣A∩B∣.

A glance at a Venn diagram makes this self-evident. The sum ∣A∣+∣B∣ counts every element of A∩B twice, so we subtract ∣A∩B∣ once to correct the overcount.

2 The general formula

Theorem 2 (Inclusion–exclusion, general form).
For finite sets A1​,A2​,…,An​,
​i=1⋃n​Ai​​=k=1∑n​(−1)k+1∣S∣=k∑​​i∈S⋂​Ai​​
where S ranges over all non-empty subsets of {1,2,…,n}.

Written out explicitly:

​i=1⋃n​Ai​​=i∑​∣Ai​∣−i<j∑​∣Ai​∩Aj​∣+i<j<k∑​∣Ai​∩Aj​∩Ak​∣−⋯+(−1)n+1∣A1​∩⋯∩An​∣.

3 Proof via indicator functions

Proof.
Fix an arbitrary element x∈⋃i=1n​Ai​. Suppose x belongs to exactly m of the sets, say Ai1​​,…,Aim​​ (where m≥1).

We compute how many times x is counted by the right-hand side. For x to lie in the intersection ⋂i∈S​Ai​, every index in S must be among {i1​,…,im​}. The number of subsets S of size k drawn from {i1​,…,im​} is (km​).

Thus the total count of x on the right-hand side is
k=1∑m​(−1)k+1(km​)=1−k=0∑m​(−1)k(km​)=1−(1−1)m=1
where we applied the binomial theorem. Every element of the union is counted exactly once, and the identity follows. □
Remark 3.
The heart of the proof is the identity (1−1)m=0 for m≥1. This special case of the binomial theorem is what guarantees the perfect cancellation of all overcounting. It is the engine that makes inclusion–exclusion work.

4 Derangements

Definition 4 (Derangement).
A permutation σ of {1,2,…,n} satisfying σ(i)=i for every i is called a derangement. The number of derangements is denoted Dn​.
Theorem 5.
Dn​=n!k=0∑n​k!(−1)k​.
Proof.
Let Ai​={σ∈Sn​∣σ(i)=i} be the set of permutations that fix i. Then Dn​=n!−∣A1​∪A2​∪⋯∪An​∣.

For any subset S of size k, the intersection ⋂i∈S​Ai​ consists of all permutations that fix every element of S. The remaining n−k elements may be permuted freely, so ∣⋂i∈S​Ai​∣=(n−k)!.

By inclusion–exclusion,
∣A1​∪⋯∪An​∣=k=1∑n​(−1)k+1(kn​)(n−k)!

Therefore,
Dn​=n!−k=1∑n​(−1)k+1(kn​)(n−k)!=k=0∑n​(−1)k(kn​)(n−k)!=n!k=0∑n​k!(−1)k​.
□
Remark 6.
As n grows, Dn​/n! converges to 1/e≈0.3679. In other words, the probability that a uniformly random permutation is a derangement is roughly 36.8%, essentially independent of n. This is a beautiful instance of a discrete quantity converging to a value expressed in terms of e.

5 A competitive-programming classic: integers coprime to given primes

Example 7 (Counting integers not divisible by any of p1​,…,pk​).
Let Ai​={m∣1≤m≤n,pi​∣m}, so ∣Ai​∣=⌊n/pi​⌋. If p1​,…,pk​ are distinct primes, then ∣⋂i∈S​Ai​∣=⌊n/∏i∈S​pi​⌋.

By inclusion–exclusion, the number of integers in {1,…,n} divisible by none of p1​,…,pk​ is
n−​i=1⋃k​Ai​​=n−k=1∑n​(−1)k+1∣S∣=k∑​⌊∏i∈S​pi​n​⌋.
This is also the computation underlying Euler's totient function φ(n).
Remark 8.
For k prime factors, one must enumerate all 2k subsets. When k≤20, this amounts to roughly 220≈106 operations – perfectly tractable. For larger values of k, alternative approaches are needed.

6 A glimpse of Mobius inversion

Remark 9.
Inclusion–exclusion is, in fact, a special case of the Mobius inversion formula. On a partially ordered set (P,≤), if functions f and g satisfy g(x)=∑y≤x​f(y), then f can be recovered via f(x)=∑y≤x​μ(x,y)g(y), where μ is the Mobius function of the poset. On the Boolean lattice (the power set ordered by inclusion), the Mobius function is μ(S,T)=(−1)∣S∖T∣– and this is precisely the source of the alternating signs in inclusion–exclusion.

7 Takeaway

  • Inclusion–exclusion ensures that every element is counted exactly once, with the cancellation guaranteed by the binomial identity (1−1)m=0.

  • Derangements provide a beautiful application: Dn​/n!→1/e.

  • In competitive programming, the principle yields a standard method for counting integers satisfying divisibility constraints.

  • Inclusion–exclusion is a special case of Mobius inversion on partially ordered sets, opening the door to a far more general theory.

In the next article, we explore the idea of encoding a sequence as a function – the generating function – and discover how it transforms recurrences into algebra.

CombinatoricsMathematicsBetween the Lines
FO
Folio Official

Mathematics "between the lines" — exploring the intuition textbooks leave out, written in LaTeX on Folio.

1 followers·107 articles
Combinatorics — Between the LinesPart 3 of 8
Previous
The many faces of the binomial coefficient: why nCr is so ubiquitous
Next
Recurrences and generating functions: what happens when you turn a sequence into a function

Share your expertise with the world

Write articles with LaTeX support, build your audience, and earn from your knowledge.

Start Writing — It's Free

More from Folio Official

Folio Official·March 1, 2026

Recurrences and generating functions: what happens when you turn a sequence into a function

A generating function is the "business card" of a sequence. Multiplication becomes convolution, and recurrences become algebraic equations. We derive the closed form for the Fibonacci numbers (Binet's formula) as a showcase of the method.

CombinatoricsMathematicsBetween the Lines
Folio Official·March 1, 2026

Combinatorics meets dynamic programming: when counting becomes computation

Dynamic programming is bottom-up evaluation of a recurrence; a generating function is its analytic closed form. We draw the correspondence between the knapsack DP and generating function multiplication, discuss convolution and FFT, and survey the connections across the entire series.

CombinatoricsMathematicsBetween the Lines
Folio Official·March 1, 2026

Counting under symmetry: Burnside's lemma and Polya's enumeration theorem

When objects related by rotation or reflection are considered identical -- as with necklaces -- naive counting fails. Using the language of group actions, Burnside's lemma reduces the problem to a clean formula: the number of distinct objects equals the average number of fixed points across the group.

CombinatoricsMathematicsBetween the Lines
Folio Official·March 1, 2026

What does it mean to count? Addition, multiplication, and surjections

Counting is the act of measuring the cardinality of a set. Whether you add or multiply depends on whether your set decomposes as a disjoint union or a Cartesian product. From this single viewpoint, permutations, combinations, and the surjection formula all emerge naturally.

CombinatoricsMathematicsBetween the Lines