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 :
Two overlapping sets: the middle region is counted once in and once in , so we subtract .
Of people, like coffee, like tea, and like both. How many like at least one drink? How many like neither?
Three sets: singles pairs triple. Each atomic region ends up counted exactly once.
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 .
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.
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.
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:
Tip: “Exactly people get their own item” : choose who is fixed, then derange the rest. Summing over recovers , a useful sanity check.
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 .
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.
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 .
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.
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.
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-in-a-pair and one divides the other (write each as ; there are only odd parts --- the boxes).
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)?
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 plus the entry up-and-left.
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 .
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 from to ; the ratings sum to . Show some rating value was given by at least people is false in general, but the averaging principle still bites: the mean rating is , so some person rated it at least and some rated it at most . More usefully, distributing “rating-points” among possible values, some value carries at least ? No --- points are not people. The correct averaging statement: with people and scores, some score was chosen by at least people.
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.
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.