Two– and Three–Set Inclusion–Exclusion
When two sets overlap, adding their sizes counts the overlap twice, so subtract it once:
For three sets, add the singles, subtract the pairs, add back the triple:
To count elements in none of the sets, subtract the union from the universe :
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 . The pigeonhole principle is the opposite kind of tool: it proves something must exist ( objects in boxes force a shared box) without finding it.
Take the survey below: like coffee, like tea, like both. The in the middle are inside both circles, so counts them twice; subtracting once leaves each person counted once.
Of people, like coffee, like tea, and like both. How many like at least one drink? How many like neither?
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 lies in all three circles: it is counted times among the singles, removed 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.
How many integers from to are divisible by , , or ?
Tip: The count divisible by both and is the count divisible by — not , unless and are coprime. For and , use , not .
In a class of students, play soccer and play tennis; play both. How many play at least one of the two sports?
Work it on paper before revealing the answer.
Show the answerHide the answer
Answer: Let be the set of soccer players and the set of tennis players. We are given , , and . “At least one” means the union . By inclusion–exclusion,
We subtract the overlap once because the students who play both were counted in both and .
General Inclusion–Exclusion
For sets , alternate sums over single sets, pairs, triples, and so on:
Equivalently, the number of elements in none of the sets is
where the empty intersection is the whole universe .
How many integers from to are divisible by none of , , ?
Sign pattern: the term for an intersection of sets carries sign in the union formula (add odd, subtract even). In the “none” formula it is . Miscounting signs is the #1 error — write the layers out explicitly.
Two sets satisfy , , and . Find .
Work it on paper before revealing the answer.
Show the answerHide the answer
Answer: Apply the two-set inclusion–exclusion formula directly:
Derangements
A derangement is a permutation in which no element stays in its original position. Inclusion–exclusion over the “ is fixed” events gives
Two handy recurrences: and
As grows, , so a random permutation is a derangement about of the time.
Where the recurrence comes from: person takes someone else's hat, say person 's, in ways. Either takes hat back (a swap), and the other people must be deranged among themselves, ways; or refuses hat , and then hat plays the role of 's own forbidden hat, so the people other than form a derangement, ways. The figure lists the derangements of this way.
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?
Check with the recurrence:
Subtracting the fixed points only once. "All permutations minus those that fix some person" is tempting as for , which is absurd: fixes nobody. The counts a permutation fixing two people twice, one fixing all four four times. Inclusion–exclusion repairs exactly this: .
Tip: “Exactly people get their own item” : choose who is fixed, then derange the rest. Summing over recovers , a useful sanity 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 answerHide the answer
Answer: This is . By the recurrence, ; by the formula, .
Counting Surjections (Onto Functions)
The number of surjections (onto functions) from an –element set onto a –element set is found by excluding the maps that miss at least one target value:
This also counts the ways to distribute distinct objects into distinct boxes with no box empty. It relates to Stirling numbers of the second kind by
where counts partitions of the objects into unlabeled nonempty groups.
How many functions from onto are there?
Equivalently .
Trap: counts all functions; only are onto. If there are surjections (too few inputs to hit every output), and the alternating sum indeed collapses to .
In how many ways can different toys be given to children so that each child gets at least one toy?
Work it on paper before revealing the answer.
Show the answerHide the answer
Answer: Each toy picks a child: ways. Subtract the ways in which one child gets everything. That is inclusion–exclusion with : .
The Pigeonhole Principle
If objects are placed into boxes and , 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 , 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 leave remainders on division by : the box for remainder gets both and , and their difference is a multiple of .
Show that among any integers, two leave the same remainder when divided by . The possible remainders are — four boxes. With integers (five objects), two must share a remainder. Their difference is then divisible by .
Choosing boxes that do not force what you want. To show that among any integers two have a difference divisible by , sorting by parity (two boxes) does force two integers into a box, but two numbers of equal parity only differ by a multiple of : and share a box and differ by . The boxes have to be the classes of the property you are after, here remainders mod .
Tip: “Two share a remainder mod ” is the same as “their difference is a multiple of .” This turns many divisibility claims into a pigeonhole count with boxes.
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 answerHide the answer
Answer: The boxes are the days. Seven people can all be born on different days, so is not enough. With people in boxes, some box holds two. The answer is .
Generalized Pigeonhole & Extremal Arguments
If objects go into boxes, then some box contains at least
objects. (If every box had at most , the total would be under .) Turned around, to force some box to hold at least objects you need , i.e. 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.
A drawer holds socks in colors. How many must you draw to be sure of of one color?
Here , , so .
What is the largest subset of containing no two elements that sum to ? Pair them: — pairs. From each pair you may keep at most one number, so the largest safe subset has size (e.g. ).
Strategy: “How many to guarantee ?” means build the worst case ( 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.
A school has students in a class. What is the largest number such that some birth month is certain to contain at least of them?
Work it on paper before revealing the answer.
Show the answerHide the answer
Answer: students in month-boxes: some box holds at least , since . And is the most you can promise: students in each of months and in each of the other make with no month above .
Going Deeper: Connections & Advanced Tools
Euler's totient counts integers in coprime to . Let be the distinct primes dividing ; applying the “none of the divisibility events” form of PIE gives the product formula
For example (namely ). This is the surjection/derangement machinery in disguise: a signed sum over subsets of “forbidden” primes.
The number of ways to split labeled tasks among identical days with each day used at least once is a set partition count . Labeling the days (making them distinct) multiplies by :
The PIE formula and the Stirling–number viewpoint always agree.
Number theory. Among any integers, two are congruent modulo , so their difference is a multiple of . Among any numbers chosen from , two are consecutive (use the boxes ), and two are such that one divides the other (write each number as ; there are only possible odd parts, and two numbers with the same odd part differ by a factor that is a power of ).
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. points in a unit square: cut it into four squares, so two points lie within distance .
Prove that among any integers, two have a difference divisible by . The residues modulo form boxes . Twelve integers into eleven residue classes force two into the same class; their difference is then divisible by . (This is exactly .)
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.
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)?
Of students, take Math, take Physics, take Chemistry; take Math & Physics, take Math & Chemistry, take Physics & Chemistry, and take all three. How many take at least one subject?
Work it on paper before revealing the answer.
Show the answerHide the answer
Answer: Let be the sets taking Math, Physics, Chemistry. We are given
By three-set inclusion–exclusion,
Stirling Numbers of the Second Kind & Bell Numbers
The Stirling number of the second kind counts the ways to partition an –element set into exactly nonempty, unlabeled blocks. Building an element one at a time gives the recurrence
(the new element either joins one of the existing blocks, or starts a fresh block with the previous elements split into blocks). Boundary values: , , .
Labeling the blocks turns each partition into a surjection, recovering the count from the previous section:
Summing over all block-counts gives the Bell number , the total number of partitions of an –set.
Each entry is times the entry directly above it plus the entry up and to the left: . The row sums are the Bell numbers .
In how many ways can distinct prizes be split among indistinguishable nonempty groups? This is . Build it from the recurrence, using and :
If the groups were instead labeled (say, Room , Room , Room ), multiply by :
Labeled vs. unlabeled: identical boxes/groups use ; distinct boxes use . If empty boxes are allowed, sum over the number of nonempty boxes: distinct boxes give , identical boxes give .
In how many ways can people be split into nonempty groups, where the groups have no names?
Work it on paper before revealing the answer.
Show the answerHide the answer
Answer: This is . Directly: each split is a subset and its complement, and the nonempty proper subsets pair up into splits (a set and its complement give the same split). Or . By shape: splits of type and of type .
Derangement Asymptotics: The Nearest–Integer Rule
The Maclaurin series truncated at is exactly . The leftover tail is tiny, so
Bounding the alternating tail gives for , which is why rounding is always correct. Consequently the probability that a random permutation of objects is a derangement,
converges extraordinarily fast — already at we have , agreeing with to three decimals.
Compute without the full alternating sum. Since and ,
Cross-check with :
Contest shortcut: to get for larger , apply starting from ; the “” alternates. The nearest-integer fact is the fast estimate, the recurrence is the exact check.
Exactly Sets: The Bonferroni Refinement
Let be the -th symmetric sum of intersection sizes (). The number of elements lying in exactly of the sets is
and the number in at least of the sets is
Setting in gives the “none” formula; in gives the ordinary union. Bonferroni inequalities: truncating the alternating series after an odd number of terms over-estimates and after an even number under-estimates it, so partial sums bracket the true count.
For the hat–check problem with people, how many return arrangements give exactly people their own hat? Using the “exactly ” idea for fixed points (choose the fixed pair, derange the rest):
As a check that the counts partition :
Of integers to , how many are divisible by exactly two of ? Here and . Then
Why the binomials appear: an element in exactly sets is counted times inside ; the alternating -weighted sum cancels all and leaves each “exactly-” element counted once. Memorize the two coefficients: for exactly, for at least.
Ramsey Numbers & Probabilistic Pigeonhole
Color the edges of a complete graph on vertices with two colors (say, acquainted / stranger). A monochromatic triangle is three mutually-acquainted or three mutual strangers. The Ramsey number is the least forcing one to exist.
: Fix a vertex among . Its edges get colors, so by pigeonhole () at least share a color — say is acquainted with . If any edge among is “acquainted,” that edge with forms an acquainted triangle; otherwise are mutual strangers — a stranger triangle. Either way a monochromatic triangle appears.
: color the -cycle's edges red and its “diagonals” blue; neither color contains a triangle. Hence : any party of has mutual acquaintances or mutual strangers.
The pentagon (solid) and pentagram (dashed) each avoid a triangle, proving vertices are not enough.
people each rate a film with a whole number from to , and the ratings add up to . (a) Show that someone rated the film at least . (b) Show that some rating value was given by at least people.
Solution. (a) The mean rating is . If everyone had rated or less, the total would be at most . So someone rated above the mean, and a whole number above is at least . (b) This is pigeonhole with the people as pigeons and the values as boxes: some value was chosen by at least people.
The two parts average different things: (a) averages ratings over people, (b) averages people over values. The total plays no part in (b).
Each of scientists corresponds with every other on one of topics. Prove some three of them discuss a single common topic (i.e. ). Fix a scientist ; its edges use topics, so by pigeonhole edges share a topic — say to the group of people. If any pair inside also uses topic , they and form a –triangle. Otherwise uses only the other topics, and a -vertex -colored graph contains a monochromatic triangle by . Either way a monochromatic triangle exists.
The averaging principle: in any finite collection, some element is at least the average and some is at most the average. Combined with pigeonhole ( 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 of the sets is counted times, by the binomial theorem. The pigeonhole principle is the contrapositive of "if each of boxes has at most one object, there are at most objects". Its power is in choosing the boxes: to show that among any numbers from to two differ by exactly , use the boxes for .
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
What it means. Add, subtract the overlaps, add back the triple overlap. With more items than boxes, some box repeats.
Example. Among people and 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 , removed , added — again net one. Pigeonhole: if every one of boxes held fewer than , the total would fall short of .
Tip. Alternate the signs by set size: odd-sized intersections add, even-sized subtract.