Module 2.3 · Counting and Probability
Inclusion-exclusion
When you count the members of overlapping groups by adding the group sizes, anything in two groups gets counted twice. Inclusion-exclusion is the bookkeeping that fixes this: add the singles, subtract the doubles, add back the triples, and so on. It's the engine behind derangements, "every box nonempty" problems, and upper bounds in stars and bars.
Two and three sets
For two sets, an element in both and is counted twice by , so subtract it once:
For three sets, draw the Venn diagram.
Adding counts each two-set region twice and the center three times. Subtracting the three pairwise intersections removes the double counts, but it also removes the center three times, leaving it counted times. So add it back once:
Inclusion-exclusion
To count elements with at least one of several properties, add the sizes of the single sets, subtract the sizes of all pairwise intersections, add all triple intersections, subtract all quadruple intersections, and so on, alternating signs.
To count elements with none of the properties, subtract that from the total:
Why it works. Take an element that has exactly of the properties. It is counted times in the singles, times in the pairs, and so on. Its net count is
because . Every element is counted exactly once.
The symmetric shortcut
In contest problems the sets are usually symmetric: every single set has the same size, every pair has the same intersection size, and so on. Then with properties,
where is the size of any one -fold intersection. The work is just computing .
Worked example: Divisibility
How many integers from to are divisible by , , or ?
Count multiples with floors: , , . The pairwise intersections are multiples of , , : , , . Multiples of : .
Worked example: Derangements
Five people check their hats, and the hats are handed back at random. In how many of the ways does nobody get their own hat?
Let be the arrangements where person gets their own hat. Fixing any specific people leaves arrangements of the rest. So
The general pattern is . Worth memorizing: , , , , , .
Worked example: No empty box
In how many ways can different balls be placed into different boxes so that no box is empty?
Total: . Let be the placements where box is empty. One specific box empty: . Two specific boxes empty: . All three empty: .
Common mistake
The most common error is stopping after the subtraction step. In the example above, is wrong: the placements with all balls in one box were subtracted twice (once for each empty box) and must be added back. Always ask, "what did I subtract more than once?"
Worked example: No two identical letters adjacent
How many arrangements of AABBCC have no two identical letters next to each other?
Total: . Let be the arrangements with the two A's adjacent, and similarly and .
- One pair glued (say AA as a block): arrange AA, B, B, C, C, giving .
- Two pairs glued: arrange AA, BB, C, C, giving .
- All three glued: .
Practice
How many integers from to are divisible by neither nor ?
Enter a number. Fractions like 3/4 and sqrt(2) are OK.
In a class of students, play soccer, play basketball, and play neither. How many play both?
How many integers from to are neither perfect squares nor perfect cubes?
Enter a number. Fractions like 3/4 and sqrt(2) are OK.
Six letters are placed at random into six addressed envelopes, one per envelope. In how many of the ways do exactly two letters land in their correct envelopes?
Six students are assigned to three different project rooms. Each student goes to one room, and each room must get at least one student. How many assignments are possible?
Enter a number. Fractions like 3/4 and sqrt(2) are OK.
How many strings of digits (each from to , leading zeros allowed) contain at least one , at least one , and at least one ?
How many integers from to share no prime factor with ?
Enter a number. Fractions like 3/4 and sqrt(2) are OK.
How many solutions does have in nonnegative integers with every ?
Four married couples sit in a row of chairs. In how many seatings does no one sit next to their spouse?
Real contest practice
- 2021 AMC 10B, Problem 22: a probability that at least one box gets matching colors, solved with inclusion-exclusion.
- 2015 AMC 12A: Problem 17 asks for the chance that no two neighbors at a round table both stand. Try it by subtracting the "some adjacent pair stands" outcomes with inclusion-exclusion.