Folioby Interconnected
Log InSign Up

The Inclusion--Exclusion Principle: Counting by Alternating Sums

We prove the inclusion--exclusion principle and apply it to derive the formula for the number of derangements D_n, Euler's totient function, the relationship between surjections and Stirling numbers of the second kind, and the M\"{o}bius inversion formula.

FO
Folio Official
March 1, 2026

1 The Inclusion–Exclusion Principle

Theorem 1 (Inclusion–exclusion principle).
For finite sets A1​,A2​,…,An​,
​i=1⋃n​Ai​​=k=1∑n​(−1)k−11≤i1​<⋯<ik​≤n∑​∣Ai1​​∩⋯∩Aik​​∣.
Proof.
We show that each element x∈⋃i=1n​Ai​ contributes exactly 1 to the right-hand side. Suppose x belongs to exactly m of the sets Ai1​​,…,Aim​​. Its contribution to the right-hand side is
k=1∑m​(−1)k−1(km​)=1−k=0∑m​(−1)k(km​)=1−(1−1)m=1
since m≥1 ensures (1−1)m=0. Hence every element is counted exactly once. □

2 Derangements

Definition 2 (Derangement).
A permutation σ of {1,2,…,n} satisfying σ(i)=i for all 1≤i≤n is called a derangement. The number of derangements is denoted Dn​.
Theorem 3.
Dn​=n!k=0∑n​k!(−1)k​
Proof.
Define Ai​={σ∈Sn​∣σ(i)=i}. Then Dn​=n!−∣A1​∪⋯∪An​∣. Since ∣Ai1​​∩⋯∩Aik​​∣=(n−k)!, the inclusion–exclusion principle gives
∣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​.
□
Corollary 4.
Dn​ is the nearest integer to n!/e. That is, Dn​=⌊n!/e+1/2⌋.

3 Euler's Totient Function

Theorem 5.
Let the prime factorization of a positive integer n be n=p1a1​​p2a2​​⋯pkak​​. Then
φ(n)=ni=1∏k​(1−pi​1​).
Proof.
Define Ai​={m∈{1,…,n}∣pi​∣m}. Then φ(n)=n−∣A1​∪⋯∪Ak​∣. Since ∣Ai1​​∩⋯∩Aij​​∣=n/(pi1​​⋯pij​​), inclusion–exclusion yields
φ(n)=n−i∑​pi​n​+i<j∑​pi​pj​n​−⋯=ni=1∏k​(1−pi​1​).
□

4 Surjections and Stirling Numbers of the Second Kind

Definition 6 (Stirling number of the second kind).
The number of ways to partition an n-element set into k nonempty subsets is called a Stirling number of the second kind, denoted S(n,k) or {kn​}.
Theorem 7.
S(n,k)=k!1​j=0∑k​(−1)j(jk​)(k−j)n
Proof.
Recall that the number of surjections from an n-element set A onto a k-element set B is ∑j=0k​(−1)j(jk​)(k−j)n (established previously). A surjection f:A→B corresponds to a partition of A into k blocks together with a labeling of these blocks by the elements of B. Since there are k! such labelings, we obtain S(n,k)=(number of surjections)/k!. □

5 The Möbius Inversion Formula

Definition 8 (M\"obius function).
The Möbius functionμ:Z>0​→{−1,0,1} is defined by
μ(n)=⎩⎨⎧​1(−1)k0​n=1n=p1​p2​⋯pk​ (a product of k distinct primes)otherwise (i.e., n has a squared prime factor)​
Theorem 9 (M\"obius inversion formula).
For arithmetic functions f and g,
g(n)=d∣n∑​f(d)⟺f(n)=d∣n∑​μ(n/d)g(d).
Proof.
Substitute g(d)=∑e∣d​f(e) into the right-hand side:
d∣n∑​μ(n/d)e∣d∑​f(e)=e∣n∑​f(e)d∣ne∣d​∑​μ(n/d).
Setting d=ek, the inner sum becomes ∑k∣(n/e)​μ(n/(ek)). Writing m=n/e, this equals ∑l∣m​μ(l), which is a well-known identity: it equals 1 when m=1 and 0 when m>1. Hence the right-hand side reduces to f(n). □
Example 10.
Taking g(n)=n (the identity function), the identity n=∑d∣n​φ(d) holds. By the Möφ(n)=∑d∣n​μ(n/d)⋅d.
CombinatoricsDiscrete MathematicsTextbookInclusion-ExclusionMobius Inversion
FO
Folio Official

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

1 followers·107 articles
Combinatorics TextbookPart 4 of 13
Previous
Binomial Coefficients and the Binomial Theorem: From Pascal's Triangle to the Multinomial Theorem
Next
Generating Functions I (Ordinary): Encoding Sequences as Power Series

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

Recurrence Relations and Counting: Solving Linear Recurrences

We present the method of characteristic equations for solving linear recurrences with constant coefficients, treat the case of repeated roots, discuss nonhomogeneous recurrences, establish the equivalence with generating functions, and introduce matrix exponentiation and the Kitamasa method.

CombinatoricsDiscrete MathematicsTextbook
Folio Official·March 1, 2026

Combinatorial Designs and Latin Squares: Balanced Arrangements

We define Latin squares and prove their existence, introduce mutually orthogonal Latin squares (MOLS), develop the theory of balanced incomplete block designs (BIBDs), prove Fisher's inequality, and discuss the connection with finite projective planes.

CombinatoricsDiscrete MathematicsTextbook
Folio Official·March 1, 2026

Burnside's Lemma and P\'olya Enumeration: Counting Under Symmetry

Starting from the definitions of group actions, orbits, and stabilizers, we prove Burnside's lemma (the Cauchy--Frobenius theorem), introduce the cycle index of a permutation group, and derive P\'olya's enumeration theorem. Applications to necklace and bracelet counting are given.

CombinatoricsDiscrete MathematicsTextbook
Folio Official·March 1, 2026

Combinatorics: A Complete Summary of Definitions, Theorems, and Proofs

A single-page survey of undergraduate combinatorics. Covers the fundamentals of counting, binomial coefficients, inclusion-exclusion, generating functions, Catalan numbers, Ramsey theory, Burnside and Polya enumeration, combinatorial designs, posets, and algebraic combinatorics. Includes a dependency diagram and a theorem-algorithm directory.

CombinatoricsDiscrete MathematicsSummary