Inclusion–Exclusion, Derangements & Pigeonhole

Study Sheet

Inclusion–Exclusion, Derangements & Pigeonhole

Competition counting & probability, Unit 5

Two– and Three–Set Inclusion–Exclusion

Concept
Counting a Union Without Double–Counting

When two sets overlap, adding their sizes counts the overlap twice, so subtract it once:

 ∣A∪B∣=∣A∣+∣B∣−∣A∩B∣ \boxed{\,|A\cup B| = |A| + |B| - |A\cap B|\,}

For three sets, add the singles, subtract the pairs, add back the triple:

∣A∪B∪C∣=∣A∣+∣B∣+∣C∣−∣A∩B∣−∣A∩C∣−∣B∩C∣+∣A∩B∩C∣.|A\cup B\cup C| = |A|+|B|+|C| - |A\cap B| - |A\cap C| - |B\cap C| + |A\cap B\cap C|.

To count elements in none of the sets, subtract the union from the universe ∣U∣|U|:

∣A‾∩B‾∣=∣U∣−∣A∪B∣.|\overline{A}\cap\overline{B}| = |U| - |A\cup B|.

Inclusion–exclusion counts a union by adding the sets, subtracting the pairwise overlaps, adding back the triple overlaps, and so on; derangements (permutations with no fixed point) are its classic application, giving Dn≈n!/eD_n \approx n!/e. The pigeonhole principle is the opposite kind of tool: it proves something must exist (n+1n + 1 objects in nn boxes force a shared box) without finding it.

Take the survey below: 3030 like coffee, 2525 like tea, 1010 like both. The 1010 in the middle are inside both circles, so 30+2530 + 25 counts them twice; subtracting 1010 once leaves each person counted once.

Example
Example: A Survey

Of 5050 people, 3030 like coffee, 2525 like tea, and 1010 like both. How many like at least one drink? How many like neither?

∣C∪T∣=30+25−10=45.Neither=50−45=5.|C\cup T| = 30 + 25 - 10 = \mathbf{45}.\qquad \text{Neither} = 50 - 45 = \mathbf{5}.
Side note
Where it goes wrong

Stopping inclusion–exclusion after subtracting the pairwise overlaps. With three or more sets, the triple overlaps were subtracted too many times and must be added back.

Three sets: ++ singles −- pairs ++ triple. The diagram is the divisibility example below. The number 3030 lies in all three circles: it is counted 33 times among the singles, removed 33 times among the pairs, and added back once with the triple, so it is counted exactly once. Every other region works out the same way.

Example
Example: Divisibility

How many integers from 11 to 3030 are divisible by 22, 33, or 55?

⌊302⌋=15, ⌊303⌋=10, ⌊305⌋=6;⌊306⌋=5, ⌊3010⌋=3, ⌊3015⌋=2;⌊3030⌋=1.\left\lfloor\tfrac{30}{2}\right\rfloor=15,\ \left\lfloor\tfrac{30}{3}\right\rfloor=10,\ \left\lfloor\tfrac{30}{5}\right\rfloor=6;\quad \left\lfloor\tfrac{30}{6}\right\rfloor=5,\ \left\lfloor\tfrac{30}{10}\right\rfloor=3,\ \left\lfloor\tfrac{30}{15}\right\rfloor=2;\quad \left\lfloor\tfrac{30}{30}\right\rfloor=1.
15+10+6−5−3−2+1=22.15+10+6 - 5-3-2 + 1 = \mathbf{22}.
Tip

Tip: The count divisible by both aa and bb is the count divisible by lcm⁡(a,b)\operatorname{lcm}(a,b) — not abab, unless aa and bb are coprime. For 66 and 1010, use lcm⁡=30\operatorname{lcm}=30, not 6060.

Try it
Quick check

In a class of 3030 students, 1818 play soccer and 1212 play tennis; 77 play both. How many play at least one of the two sports?

Work it on paper before revealing the answer.

Show the answer

Answer: Let SS be the set of soccer players and TT the set of tennis players. We are given ∣S∣=18|S|=18, ∣T∣=12|T|=12, and ∣S∩T∣=7|S\cap T|=7. “At least one” means the union S∪TS\cup T. By inclusion–exclusion,

∣S∪T∣=∣S∣+∣T∣−∣S∩T∣=18+12−7=23.|S\cup T|=|S|+|T|-|S\cap T|=18+12-7=23.

We subtract the overlap once because the 77 students who play both were counted in both ∣S∣|S| and ∣T∣|T|.

23\boxed{23}

General Inclusion–Exclusion

Concept
The Full Formula

For sets A1,…,AnA_1,\dots,A_n, alternate sums over single sets, pairs, triples, and so on:

∣ ⋃i=1nAi ∣=∑i∣Ai∣−∑i<j∣Ai∩Aj∣+∑i<j<k∣Ai∩Aj∩Ak∣−⋯+(−1)n+1∣A1∩⋯∩An∣.\left|\,\bigcup_{i=1}^{n} A_i\,\right| = \sum_i |A_i| - \sum_{i<j}|A_i\cap A_j| + \sum_{i<j<k}|A_i\cap A_j\cap A_k| - \cdots + (-1)^{n+1}\bigl|A_1\cap\cdots\cap A_n\bigr|.

Equivalently, the number of elements in none of the sets is

∣A1‾∩⋯∩An‾∣=∑S⊆{1,…,n}(−1)∣S∣∣⋂i∈SAi∣,\bigl|\overline{A_1}\cap\cdots\cap\overline{A_n}\bigr| = \sum_{S\subseteq\{1,\dots,n\}} (-1)^{|S|}\Bigl|\bigcap_{i\in S} A_i\Bigr|,

where the empty intersection is the whole universe ∣U∣|U|.

Example
Example: Coprime–Style Count

How many integers from 11 to 100100 are divisible by none of 22, 33, 55?

∣U∣=100;50+33+20 (singles),16+10+6 (pairs: 6,10,15),3 (triple: 30).|U|=100;\quad 50+33+20 \ (\text{singles}),\quad 16+10+6 \ (\text{pairs: } 6,10,15),\quad 3 \ (\text{triple: }30).
100−(50+33+20)+(16+10+6)−3=100−103+32−3=26.100 - (50+33+20) + (16+10+6) - 3 = 100 - 103 + 32 - 3 = \mathbf{26}.
Tip

Sign pattern: the term for an intersection of mm sets carries sign (−1)m+1(-1)^{m+1} in the union formula (add odd, subtract even). In the “none” formula it is (−1)m(-1)^{m}. Miscounting signs is the #1 error — write the layers out explicitly.

Try it
Quick check

Two sets satisfy ∣A∣=20|A|=20, ∣B∣=15|B|=15, and ∣A∩B∣=6|A\cap B|=6. Find ∣A∪B∣|A\cup B|.

Work it on paper before revealing the answer.

Show the answer

Answer: Apply the two-set inclusion–exclusion formula directly:

∣A∪B∣=∣A∣+∣B∣−∣A∩B∣=20+15−6=29.|A\cup B|=|A|+|B|-|A\cap B|=20+15-6=29.
29\boxed{29}

Derangements

Concept
Permutations With No Fixed Point

A derangement is a permutation in which no element stays in its original position. Inclusion–exclusion over the “ii is fixed” events gives

 Dn=n!∑k=0n(−1)kk!=n!(1−11!+12!−⋯+(−1)nn!).\boxed{\,D_n = n!\sum_{k=0}^{n}\frac{(-1)^k}{k!} = n!\left(1 - \frac{1}{1!} + \frac{1}{2!} - \cdots + \frac{(-1)^n}{n!}\right).}

Two handy recurrences:  Dn=(n−1)(Dn−1+Dn−2)\ D_n = (n-1)\bigl(D_{n-1}+D_{n-2}\bigr) and  Dn=nDn−1+(−1)n.\ D_n = nD_{n-1} + (-1)^n.

n123456Dn012944265\begin{array}{c|cccccc} n & 1 & 2 & 3 & 4 & 5 & 6\\\hline D_n & 0 & 1 & 2 & 9 & 44 & 265 \end{array}

As nn grows, Dn/n!→1/e≈0.3679D_n/n! \to 1/e \approx 0.3679, so a random permutation is a derangement about 37%37\% of the time.

Where the recurrence Dn=(n−1)(Dn−1+Dn−2)D_n = (n-1)(D_{n-1} + D_{n-2}) comes from: person 11 takes someone else's hat, say person jj's, in n−1n-1 ways. Either jj takes hat 11 back (a swap), and the other n−2n-2 people must be deranged among themselves, Dn−2D_{n-2} ways; or jj refuses hat 11, and then hat 11 plays the role of jj's own forbidden hat, so the n−1n-1 people other than 11 form a derangement, Dn−1D_{n-1} ways. The figure lists the 99 derangements of 44 this way.

Example
Example: The Hat–Check Problem
person 1 gets hat 2214323412413person 1 gets hat 3341231423421person 1 gets hat 4432141234312shaded: 1 and j swap, 1 way eachothers: 2 ways each; 3 × (2 + 1) = 9
Each string lists the hats received by persons 1,2,3,41,2,3,4; no digit is in its own place.

Four people check identical-looking hats; on the way out the hats are returned at random. In how many ways does nobody get their own hat?

D4=4!(1−1+12−16+124)=24⋅924=9.D_4 = 4!\left(1 - 1 + \tfrac12 - \tfrac16 + \tfrac{1}{24}\right) = 24\cdot\tfrac{9}{24} = \mathbf{9}.

Check with the recurrence: D4=3(D3+D2)=3(2+1)=9.D_4 = 3(D_3 + D_2) = 3(2+1) = 9.

Side note
Where it goes wrong

Subtracting the fixed points only once. "All permutations minus those that fix some person" is tempting as 4!−4⋅3!=24−24=04! - 4\cdot 3! = 24 - 24 = 0 for n=4n = 4, which is absurd: 21432143 fixes nobody. The 4⋅3!4\cdot 3! counts a permutation fixing two people twice, one fixing all four four times. Inclusion–exclusion repairs exactly this: 24−24+12−4+1=924 - 24 + 12 - 4 + 1 = 9.

Tip

Tip: “Exactly mm people get their own item” =(nm)Dn−m= \binom{n}{m} D_{n-m}: choose who is fixed, then derange the rest. Summing over mm recovers n!n!, a useful sanity check.

Try it
Quick check

Five letters are put at random into five addressed envelopes, one each. In how many ways does no letter reach its own envelope?

Work it on paper before revealing the answer.

Show the answer

Answer: This is D5D_5. By the recurrence, D5=4(D4+D3)=4(9+2)=44D_5 = 4(D_4 + D_3) = 4(9 + 2) = 44; by the formula, 120(1−1+12−16+124−1120)=60−20+5−1=44120\left(1 - 1 + \tfrac12 - \tfrac16 + \tfrac1{24} - \tfrac1{120}\right) = 60 - 20 + 5 - 1 = 44.   44\boxed{44}

Counting Surjections (Onto Functions)

Concept
Onto Functions via Inclusion–Exclusion

The number of surjections (onto functions) from an nn–element set onto a kk–element set is found by excluding the maps that miss at least one target value:

 Surj⁡(n,k)=∑i=0k(−1)i(ki)(k−i)n.\boxed{\,\operatorname{Surj}(n,k) = \sum_{i=0}^{k}(-1)^i\binom{k}{i}(k-i)^n.}

This also counts the ways to distribute nn distinct objects into kk distinct boxes with no box empty. It relates to Stirling numbers of the second kind by

Surj⁡(n,k)=k! S(n,k),\operatorname{Surj}(n,k) = k!\,S(n,k),

where S(n,k)S(n,k) counts partitions of the objects into kk unlabeled nonempty groups.

Example
Example: Onto a 33–Set

How many functions from {1,2,3,4}\{1,2,3,4\} onto {a,b,c}\{a,b,c\} are there?

Surj⁡(4,3)=34−(31)24+(32)14−(33)04=81−48+3−0=36.\operatorname{Surj}(4,3) = 3^4 - \binom{3}{1}2^4 + \binom{3}{2}1^4 - \binom{3}{3}0^4 = 81 - 48 + 3 - 0 = \mathbf{36}.

Equivalently 3! S(4,3)=6⋅6=363!\,S(4,3) = 6\cdot 6 = 36.

Tip

Trap: knk^n counts all functions; only Surj⁡(n,k)\operatorname{Surj}(n,k) are onto. If n<kn<k there are 00 surjections (too few inputs to hit every output), and the alternating sum indeed collapses to 00.

Try it
Quick check

In how many ways can 55 different toys be given to 22 children so that each child gets at least one toy?

Work it on paper before revealing the answer.

Show the answer

Answer: Each toy picks a child: 25=322^5 = 32 ways. Subtract the 22 ways in which one child gets everything. That is inclusion–exclusion with k=2k = 2: 25−(21)15+0=302^5 - \binom21 1^5 + 0 = 30.   30\boxed{30}

The Pigeonhole Principle

Concept
The Basic Principle

If nn objects are placed into kk boxes and n>kn > k, then some box contains at least two objects. More sharply: you cannot have every box with at most one object once the objects outnumber the boxes.

The art is choosing the right “boxes.” Common box constructions: remainders modulo mm, sign/parity classes, sub-regions of a shape, or pairs that sum to a target.

Five items in four boxes force at least one box to hold two. For instance 7,12,22,29,337, 12, 22, 29, 33 leave remainders 3,0,2,1,13, 0, 2, 1, 1 on division by 44: the box for remainder 11 gets both 2929 and 3333, and their difference 44 is a multiple of 44.

Example
Example: Same Remainder

Show that among any 55 integers, two leave the same remainder when divided by 44. The possible remainders are {0,1,2,3}\{0,1,2,3\} — four boxes. With 55 integers (five objects), two must share a remainder. Their difference is then divisible by 44.

Side note
Where it goes wrong

Choosing boxes that do not force what you want. To show that among any 55 integers two have a difference divisible by 44, sorting by parity (two boxes) does force two integers into a box, but two numbers of equal parity only differ by a multiple of 22: 33 and 55 share a box and differ by 22. The boxes have to be the classes of the property you are after, here remainders mod 44.

Tip

Tip: “Two share a remainder mod mm” is the same as “their difference is a multiple of mm.” This turns many divisibility claims into a pigeonhole count with mm boxes.

Try it
Quick check

What is the smallest number of people that guarantees two of them were born on the same day of the week?

Work it on paper before revealing the answer.

Show the answer

Answer: The boxes are the 77 days. Seven people can all be born on different days, so 77 is not enough. With 88 people in 77 boxes, some box holds two. The answer is 8\boxed{8}.

Generalized Pigeonhole & Extremal Arguments

Concept
The Sharpened Count

If nn objects go into kk boxes, then some box contains at least

⌈nk⌉\boxed{\left\lceil \frac{n}{k} \right\rceil}

objects. (If every box had at most ⌈n/k⌉−1\lceil n/k\rceil - 1, the total would be under nn.) Turned around, to force some box to hold at least rr objects you need n>k(r−1)n > k(r-1), i.e. n=k(r−1)+1n = k(r-1)+1 objects.

Extremal principle: to find the largest “bad-free” set, group elements into forbidden pairs/blocks; you may take at most one representative per block, and the pigeonhole bound is tight when you take exactly one.

Example
Example: Guaranteeing a Color
color 1color 2color 3color 4color 516thworst case: 3 of each = 15 socks
Fifteen socks can dodge a quartet; the sixteenth has to land on a pile of three.

A drawer holds socks in 55 colors. How many must you draw to be sure of 44 of one color?

Worst case: 3 of each color=3⋅5=15 socks, still no quartet.15+1=16.\text{Worst case: }3\text{ of each color}=3\cdot 5 = 15\text{ socks, still no quartet.}\quad 15+1 = \mathbf{16}.

Here k=5k=5, r=4r=4, so n=k(r−1)+1=5⋅3+1=16n = k(r-1)+1 = 5\cdot 3 + 1 = 16.

Example
Example: Extremal Subset
1234567891011121314151617181920
Each arc joins two numbers that sum to 2121; a safe set takes at most one end of each arc, so at most 1010 (filled dots).

What is the largest subset of {1,2,…,20}\{1,2,\dots,20\} containing no two elements that sum to 2121? Pair them: (1,20),(2,19),…,(10,11)(1,20),(2,19),\dots,(10,11) — 1010 pairs. From each pair you may keep at most one number, so the largest safe subset has size 10\mathbf{10} (e.g. {1,2,…,10}\{1,2,\dots,10\}).

Tip

Strategy: “How many to guarantee rr?” means build the worst case (r−1r-1 per box) and add one. “Largest set avoiding a condition?” means group into forbidden blocks and take one per block. These are the same pigeonhole idea read in two directions.

Try it
Quick check

A school has 4040 students in a class. What is the largest number rr such that some birth month is certain to contain at least rr of them?

Work it on paper before revealing the answer.

Show the answer

Answer: 4040 students in 1212 month-boxes: some box holds at least ⌈40/12⌉=4\lceil 40/12\rceil = 4, since 12⋅3=36<4012\cdot 3 = 36 < 40. And 44 is the most you can promise: 44 students in each of 44 months and 33 in each of the other 88 make 16+24=4016 + 24 = 40 with no month above 44.   4\boxed{4}

Going Deeper: Connections & Advanced Tools

Concept
Inclusion–Exclusion and Euler's Totient

Euler's totient φ(n)\varphi(n) counts integers in {1,…,n}\{1,\dots,n\} coprime to nn. Let p1,…,prp_1,\dots,p_r be the distinct primes dividing nn; applying the “none of the divisibility events” form of PIE gives the product formula

φ(n)=n∏i=1r(1−1pi).\varphi(n) = n\prod_{i=1}^{r}\left(1 - \frac{1}{p_i}\right).

For example φ(12)=12(1−12)(1−13)=12⋅12⋅23=4\varphi(12) = 12\left(1-\tfrac12\right)\left(1-\tfrac13\right) = 12\cdot\tfrac12\cdot\tfrac23 = 4 (namely 1,5,7,111,5,7,11). This is the surjection/derangement machinery in disguise: a signed sum over subsets of “forbidden” primes.

Example
Example: Surjections and Set Partitions

The number of ways to split 55 labeled tasks among 33 identical days with each day used at least once is a set partition count S(5,3)=25S(5,3)=25. Labeling the days (making them distinct) multiplies by 3!3!:

Surj⁡(5,3)=3! S(5,3)=6⋅25=150=35−3⋅25+3⋅15.\operatorname{Surj}(5,3) = 3!\,S(5,3) = 6\cdot 25 = 150 = 3^5 - 3\cdot 2^5 + 3\cdot 1^5.

The PIE formula and the Stirling–number viewpoint always agree.

Concept
Pigeonhole in Number Theory & Geometry

Number theory. Among any n+1n+1 integers, two are congruent modulo nn, so their difference is a multiple of nn. Among any n+1n+1 numbers chosen from {1,…,2n}\{1,\dots,2n\}, two are consecutive (use the nn boxes {1,2},{3,4},…,{2n−1,2n}\{1,2\},\{3,4\},\dots,\{2n-1,2n\}), and two are such that one divides the other (write each number as 2a⋅(odd)2^a\cdot(\text{odd}); there are only nn possible odd parts, and two numbers with the same odd part differ by a factor that is a power of 22).

Geometry. Partition a region into smaller cells; if you place more points than cells, two points share a cell and lie within its diameter. E.g. 55 points in a unit square: cut it into four 12×12\tfrac12\times\tfrac12 squares, so two points lie within distance 22\tfrac{\sqrt2}{2}.

Example
Example: Divisibility by a Difference

Prove that among any 1212 integers, two have a difference divisible by 1111. The residues modulo 1111 form 1111 boxes {0,1,…,10}\{0,1,\dots,10\}. Twelve integers into eleven residue classes force two into the same class; their difference is then divisible by 1111. (This is exactly ⌈12/11⌉=2\lceil 12/11\rceil = 2.)

Concept
The Extremal Principle

Many existence problems yield to: consider the largest, smallest, or otherwise most extreme object and argue about it. Choose the configuration maximizing (or minimizing) some quantity; if the desired property failed, you could push the extreme further — a contradiction. Paired with pigeonhole, the extremal principle proves that a structure must exist without constructing it explicitly.

Tip

Big picture. Inclusion–exclusion, derangements, surjection counts, and the totient are all one idea: count everything, then correct for over-counting with alternating signs over subsets of “bad” conditions. Pigeonhole and the extremal principle are the reverse temperament — they prove existence by counting boxes rather than configurations. Together they let you both count exactly and guarantee structure. When a direct count stalls, ask: what is being over-counted (PIE), or what are the boxes (pigeonhole)?

Try it
Quick check

Of 100100 students, 4040 take Math, 3030 take Physics, 2525 take Chemistry; 1010 take Math & Physics, 88 take Math & Chemistry, 55 take Physics & Chemistry, and 33 take all three. How many take at least one subject?

Work it on paper before revealing the answer.

Show the answer

Answer: Let M,P,CM,P,C be the sets taking Math, Physics, Chemistry. We are given

∣M∣=40, ∣P∣=30, ∣C∣=25,|M|=40,\ |P|=30,\ |C|=25,
∣M∩P∣=10, ∣M∩C∣=8, ∣P∩C∣=5, ∣M∩P∩C∣=3.|M\cap P|=10,\ |M\cap C|=8,\ |P\cap C|=5,\ |M\cap P\cap C|=3.

By three-set inclusion–exclusion,

∣M∪P∪C∣=40+30+25−10−8−5+3=75.|M\cup P\cup C|=40+30+25-10-8-5+3=75.
75\boxed{75}

Stirling Numbers of the Second Kind & Bell Numbers

Concept
Partitioning a Set into Blocks

The Stirling number of the second kind S(n,k)S(n,k) counts the ways to partition an nn–element set into exactly kk nonempty, unlabeled blocks. Building an element one at a time gives the recurrence

 S(n,k)=k S(n−1,k)+S(n−1,k−1) \boxed{\,S(n,k) = k\,S(n-1,k) + S(n-1,k-1)\,}

(the new element either joins one of the kk existing blocks, or starts a fresh block with the previous n−1n-1 elements split into k−1k-1 blocks). Boundary values: S(n,0)=[n=0]S(n,0)=[n=0], S(n,1)=S(n,n)=1S(n,1)=S(n,n)=1, S(n,2)=2n−1−1S(n,2)=2^{n-1}-1.

Labeling the blocks turns each partition into a surjection, recovering the count from the previous section:

k! S(n,k)=Surj⁡(n,k)=∑i=0k(−1)i(ki)(k−i)n.k!\,S(n,k) = \operatorname{Surj}(n,k) = \sum_{i=0}^{k}(-1)^i\binom{k}{i}(k-i)^n.

Summing over all block-counts gives the Bell number Bn=∑k=0nS(n,k)B_n=\sum_{k=0}^{n} S(n,k), the total number of partitions of an nn–set.

n0123456Bn11251552203\begin{array}{c|ccccccc} n & 0 & 1 & 2 & 3 & 4 & 5 & 6\\\hline B_n & 1 & 1 & 2 & 5 & 15 & 52 & 203 \end{array}
n\k1234561121131314176151152510161319065151\begin{array}{c|cccccc} n \backslash k & 1 & 2 & 3 & 4 & 5 & 6\\\hline 1 & 1 & & & & & \\ 2 & 1 & 1 & & & & \\ 3 & 1 & 3 & 1 & & & \\ 4 & 1 & 7 & 6 & 1 & & \\ 5 & 1 & 15 & 25 & 10 & 1 & \\ 6 & 1 & 31 & 90 & 65 & 15 & 1 \end{array}

Each entry is kk times the entry directly above it plus the entry up and to the left: S(6,3)=3⋅S(5,3)+S(5,2)=3⋅25+15=90S(6,3) = 3\cdot S(5,3) + S(5,2) = 3\cdot 25 + 15 = 90. The row sums are the Bell numbers 1,2,5,15,52,2031, 2, 5, 15, 52, 203.

Example
Example: Distinct Prizes, Identical Teams (AIME flavor)

In how many ways can 66 distinct prizes be split among 33 indistinguishable nonempty groups? This is S(6,3)S(6,3). Build it from the recurrence, using S(5,3)=25S(5,3)=25 and S(5,2)=15S(5,2)=15:

S(6,3)=3 S(5,3)+S(5,2)=3⋅25+15=90.S(6,3) = 3\,S(5,3) + S(5,2) = 3\cdot 25 + 15 = \mathbf{90}.

If the groups were instead labeled (say, Room 11, Room 22, Room 33), multiply by 3!3!: Surj⁡(6,3)=6⋅90=540=36−3⋅26+3⋅16.\operatorname{Surj}(6,3)=6\cdot 90 = 540 = 3^6 - 3\cdot 2^6 + 3\cdot 1^6.

Tip

Labeled vs. unlabeled: identical boxes/groups ⇒\Rightarrow use S(n,k)S(n,k); distinct boxes ⇒\Rightarrow use k! S(n,k)=Surj⁡(n,k)k!\,S(n,k)=\operatorname{Surj}(n,k). If empty boxes are allowed, sum over the number of nonempty boxes: distinct boxes give knk^n, identical boxes give ∑j=1kS(n,j)\sum_{j=1}^{k} S(n,j).

Try it
Quick check

In how many ways can 44 people be split into 22 nonempty groups, where the groups have no names?

Work it on paper before revealing the answer.

Show the answer

Answer: This is S(4,2)S(4,2). Directly: each split is a subset and its complement, and the 24−2=142^4 - 2 = 14 nonempty proper subsets pair up into 77 splits (a set and its complement give the same split). Or S(n,2)=2n−1−1=7S(n,2) = 2^{n-1} - 1 = 7. By shape: 44 splits of type 3+13+1 and 33 of type 2+22+2.   7\boxed{7}

Derangement Asymptotics: The Nearest–Integer Rule

Concept
DnD_n Is the Nearest Integer to n!/en!/e

The Maclaurin series e−1=∑k≥0(−1)kk!e^{-1}=\sum_{k\ge 0}\tfrac{(-1)^k}{k!} truncated at k=nk=n is exactly Dn/n!D_n/n!. The leftover tail is tiny, so

 Dn=⌊n!e+12⌋(n≥1),Dn=nearest integer to n!e.\boxed{\,D_n = \left\lfloor \frac{n!}{e} + \frac{1}{2} \right\rfloor \quad(n\ge 1),\qquad D_n = \text{nearest integer to } \frac{n!}{e}.}

Bounding the alternating tail gives ∣ Dn−n!e ∣=n!e∣∑k>n(−1)kk!∣<1n+1≤12\left|\,D_n - \tfrac{n!}{e}\,\right| = \tfrac{n!}{e}\left|\sum_{k>n}\tfrac{(-1)^k}{k!}\right| < \dfrac{1}{n+1} \le \tfrac12 for n≥1n\ge 1, which is why rounding is always correct. Consequently the probability that a random permutation of nn objects is a derangement,

Dnn!=∑k=0n(−1)kk!⟶1e≈0.367879,\frac{D_n}{n!} = \sum_{k=0}^{n}\frac{(-1)^k}{k!} \longrightarrow \frac{1}{e}\approx 0.367879,

converges extraordinarily fast — already at n=6n=6 we have D6/6!=265/720=0.368055…D_6/6! = 265/720 = 0.368055\ldots, agreeing with 1/e1/e to three decimals.

Example
Example: A Fast Derangement Count

Compute D7D_7 without the full alternating sum. Since 7!=50407! = 5040 and e≈2.718281828e\approx 2.718281828,

7!e=50402.718281828≈1854.11,D7=⌊1854.11+0.5⌋=1854.\frac{7!}{e} = \frac{5040}{2.718281828} \approx 1854.11,\qquad D_7 = \left\lfloor 1854.11 + 0.5\right\rfloor = \mathbf{1854}.

Cross-check with Dn=nDn−1+(−1)nD_n = nD_{n-1}+(-1)^n: D7=7 D6−1=7⋅265−1=1855−1=1854.D_7 = 7\,D_6 - 1 = 7\cdot 265 - 1 = 1855 - 1 = 1854.

Tip

Contest shortcut: to get DnD_n for larger nn, apply Dn=nDn−1+(−1)nD_n = nD_{n-1}+(-1)^n starting from D1=0D_1=0; the “±1\pm 1” alternates. The nearest-integer fact is the fast estimate, the recurrence is the exact check.

Exactly mm Sets: The Bonferroni Refinement

Concept
Counting Elements in Exactly (or At Least) mm Sets

Let Sj=∑∣T∣=j∣⋂i∈TAi∣S_j = \sum_{|T|=j}\bigl|\bigcap_{i\in T}A_i\bigr| be the jj-th symmetric sum of intersection sizes (S0=∣U∣S_0=|U|). The number of elements lying in exactly mm of the sets A1,…,AnA_1,\dots,A_n is

 Em=∑j=mn(−1) j−m(jm)Sj,\boxed{\,E_m = \sum_{j=m}^{n} (-1)^{\,j-m}\binom{j}{m} S_j,}

and the number in at least mm of the sets is

Lm=∑j=mn(−1) j−m(j−1m−1)Sj.L_m = \sum_{j=m}^{n} (-1)^{\,j-m}\binom{j-1}{m-1} S_j.

Setting m=0m=0 in EmE_m gives the “none” formula; m=1m=1 in LmL_m gives the ordinary union. Bonferroni inequalities: truncating the alternating series after an odd number of terms over-estimates ∣A1∪⋯∪An∣|A_1\cup\cdots\cup A_n| and after an even number under-estimates it, so partial sums bracket the true count.

Example
Example: Exactly Two Own Hats

For the hat–check problem with n=5n=5 people, how many return arrangements give exactly 22 people their own hat? Using the “exactly mm” idea for fixed points (choose the fixed pair, derange the rest):

(52)D3=10⋅2=20.\binom{5}{2} D_3 = 10\cdot 2 = \mathbf{20}.

As a check that the counts partition 5!=1205!=120:  ∑m=05(5m)D5−m=44+5⋅9+10⋅2+10⋅1+5⋅0+1=44+45+20+10+0+1=120.\ \sum_{m=0}^{5}\binom{5}{m}D_{5-m} = 44 + 5\cdot 9 + 10\cdot 2 + 10\cdot 1 + 5\cdot 0 + 1 = 44+45+20+10+0+1 = 120.

Example
Example: In Exactly Two of Three (AIME flavor)

Of integers 11 to 100100, how many are divisible by exactly two of 2,3,52,3,5? Here S2=⌊100/6⌋+⌊100/10⌋+⌊100/15⌋=16+10+6=32S_2 = \lfloor 100/6\rfloor + \lfloor 100/10\rfloor + \lfloor 100/15\rfloor = 16+10+6 = 32 and S3=⌊100/30⌋=3S_3 = \lfloor 100/30\rfloor = 3. Then

E2=(22)S2−(32)S3=32−3⋅3=32−9=23.E_2 = \binom{2}{2}S_2 - \binom{3}{2}S_3 = 32 - 3\cdot 3 = 32 - 9 = \mathbf{23}.
Tip

Why the binomials appear: an element in exactly jj sets is counted (jm)\binom{j}{m} times inside SjS_j; the alternating (jm)\binom{j}{m}-weighted sum cancels all j≠mj\ne m and leaves each “exactly-mm” element counted once. Memorize the two coefficients: (jm)\binom{j}{m} for exactly, (j−1m−1)\binom{j-1}{m-1} for at least.

Ramsey Numbers & Probabilistic Pigeonhole

Concept
Order Forced by Size: R(3,3)=6R(3,3)=6

Color the edges of a complete graph on nn vertices with two colors (say, acquainted / stranger). A monochromatic triangle is three mutually-acquainted or three mutual strangers. The Ramsey number R(3,3)R(3,3) is the least nn forcing one to exist.

R(3,3)≤6R(3,3)\le 6: Fix a vertex vv among 66. Its 55 edges get 22 colors, so by pigeonhole (⌈5/2⌉=3\lceil 5/2\rceil = 3) at least 33 share a color — say vv is acquainted with a,b,ca,b,c. If any edge among a,b,ca,b,c is “acquainted,” that edge with vv forms an acquainted triangle; otherwise a,b,ca,b,c are mutual strangers — a stranger triangle. Either way a monochromatic triangle appears.

R(3,3)>5R(3,3)>5: color the 55-cycle's edges red and its “diagonals” blue; neither color contains a triangle. Hence R(3,3)=6\boxed{R(3,3)=6}: any party of 66 has 33 mutual acquaintances or 33 mutual strangers.

The pentagon (solid) and pentagram (dashed) each avoid a triangle, proving 55 vertices are not enough.

Example
Example: An Averaging (Probabilistic) Pigeonhole

100100 people each rate a film with a whole number from 11 to 1010, and the ratings add up to 620620. (a) Show that someone rated the film at least 77. (b) Show that some rating value was given by at least 1010 people.

Solution. (a) The mean rating is 620/100=6.2620/100 = 6.2. If everyone had rated 66 or less, the total would be at most 600<620600 < 620. So someone rated above the mean, and a whole number above 6.26.2 is at least 77. (b) This is pigeonhole with the people as pigeons and the 1010 values as boxes: some value was chosen by at least ⌈100/10⌉=10\lceil 100/10\rceil = 10 people.

The two parts average different things: (a) averages ratings over people, (b) averages people over values. The total 620620 plays no part in (b).

Example
Example: Existence Beats the Average (Olympiad flavor)

Each of 1717 scientists corresponds with every other on one of 33 topics. Prove some three of them discuss a single common topic (i.e. R(3,3,3)≤17R(3,3,3)\le 17). Fix a scientist vv; its 1616 edges use 33 topics, so by pigeonhole ⌈16/3⌉=6\lceil 16/3\rceil = 6 edges share a topic TT — say to the group GG of ≥6\ge 6 people. If any pair inside GG also uses topic TT, they and vv form a TT–triangle. Otherwise GG uses only the other 22 topics, and a 66-vertex 22-colored graph contains a monochromatic triangle by R(3,3)=6R(3,3)=6. Either way a monochromatic triangle exists.

Tip

The averaging principle: in any finite collection, some element is at least the average and some is at most the average. Combined with pigeonhole (⌈n/k⌉\lceil n/k\rceil per box) and Ramsey-style edge-coloring, this proves a desired structure must exist without exhibiting it. Ramsey's slogan: complete disorder is impossible — enough size forces order.

Why It Works

Inclusion–exclusion is correct because each element of the union is counted exactly once: an element in exactly mm of the sets is counted (m1)−(m2)+(m3)−⋯=1−(1−1)m=1\binom m1 - \binom m2 + \binom m3 - \cdots = 1 - (1 - 1)^m = 1 times, by the binomial theorem. The pigeonhole principle is the contrapositive of "if each of nn boxes has at most one object, there are at most nn objects". Its power is in choosing the boxes: to show that among any 5151 numbers from 11 to 100100 two differ by exactly 5050, use the 5050 boxes {k,k+50}\{k, k + 50\} for k=1,…,50k = 1, \ldots, 50.

Reminder — The binomial theorem:  (x+y)n=∑k=0n(nk)x n−kyk(x+y)^{n}=\sum_{k=0}^{n}\binom{n}{k}x^{\,n-k}y^{k}
Tip
Reading a proof

Do not read a proof the way you read a rule. Cover the last line, ask what the previous line forces, and only then uncover it. If a step feels like magic, that is the step to work by hand with a concrete number.

Formulas, Proofs & Tips

Tip
Inclusion–exclusion and the pigeonhole principle
∣A∪B∪C∣=∑∣A∣−∑∣A∩B∣+∣A∩B∩C∣;⌈n/k⌉ in some hole|A\cup B\cup C|=\sum|A|-\sum|A\cap B|+|A\cap B\cap C|;\qquad \lceil n/k\rceil \text{ in some hole}

What it means. Add, subtract the overlaps, add back the triple overlap. With more items than boxes, some box repeats.

Example. Among 1313 people and 1212 months, two must share a birth month.

Why it works. An element in exactly two sets is counted twice by the singles and removed once by the pairs — net one. An element in all three is counted 33, removed 33, added 11 — again net one. Pigeonhole: if every one of kk boxes held fewer than ⌈n/k⌉\lceil n/k\rceil, the total would fall short of nn.

Tip. Alternate the signs by set size: odd-sized intersections add, even-sized subtract.