Counting: Inclusion–Exclusion & Stars and Bars

Study Sheet

Counting: Inclusion–Exclusion & Stars and Bars

The two workhorses of AMC counting

Inclusion–Exclusion

Tip
The principle

∣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|.

In plain terms. Add each group, subtract the double-counted overlaps, then add back what you subtracted too many times.

Example. Integers 11–100100 divisible by 2,3,2,3, or 55: 50+33+20−16−10−6+3=7450+33+20-16-10-6+3 = 74.

Why it works. Follow one element that lies in exactly kk of the sets (k≥1k \ge 1). It is added kk times by the singles, subtracted (k2)\binom k2 times by the pairs, and added back (k3)\binom k3 times by the triple, for a net count of k−(k2)+(k3)=1k - \binom k2 + \binom k3 = 1 whether kk is 11, 22 or 33. So every element of the union is counted exactly once. The alternating signs are not a patch: they are what makes each element's net contribution 11.

Example
Worked: divisible by none

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

Spotting it. "None of" is the complement of "at least one of", and "at least one of" is a union: inclusion–exclusion, then subtract from the total.

Step 1. Singles: ⌊10002⌋+⌊10003⌋+⌊10005⌋=500+333+200=1033\lfloor\tfrac{1000}{2}\rfloor + \lfloor\tfrac{1000}{3}\rfloor + \lfloor\tfrac{1000}{5}\rfloor = 500 + 333 + 200 = 1033.

Step 2. Pairs (multiples of 66, 1010, 1515): 166+100+66=332166 + 100 + 66 = 332. Triple (multiples of 3030): 3333.

Step 3. The union has 1033−332+33=7341033 - 332 + 33 = 734 elements, so 1000−734=2661000 - 734 = 266 integers are divisible by none.

Check with the density: a fraction 12⋅23⋅45=415\tfrac12 \cdot \tfrac23 \cdot \tfrac45 = \tfrac{4}{15} of each block of 3030 survives, and 415⋅1000≈266.7\tfrac{4}{15} \cdot 1000 \approx 266.7 ✓

Example
Worked: a Venn diagram with one unknown
SBT9656433
Filled from the centre outward: the seven regions add to 36, the students who play something.

In a club of 4040 students, 2222 play soccer, 1818 play basketball and 1515 play tennis. Nine play both soccer and basketball, 77 both soccer and tennis, and 66 both basketball and tennis; 44 play none of the three. How many play all three?

Spotting it. Every number in the formula is given except the triple overlap, so inclusion–exclusion is one linear equation.

Step 1. 3636 students play at least one sport, so 36=22+18+15−9−7−6+x=33+x36 = 22 + 18 + 15 - 9 - 7 - 6 + x = 33 + x.

Step 2. x=3x = 3.

Check by filling the Venn diagram from the centre out: soccer-and-basketball-only is 9−3=69 - 3 = 6, soccer-and-tennis-only 44, basketball-and-tennis-only 33; soccer-only is 22−6−4−3=922 - 6 - 4 - 3 = 9, basketball-only 66, tennis-only 55. The seven regions add to 3636 ✓

Example
Worked: using every letter

How many 55-letter strings made from the letters A, B, C use all three letters?

Spotting it. "Uses all three" is hard to count directly (the repeated letters could be anywhere). Its complement, "misses at least one letter", is a union of three simple sets.

Step 1. All strings: 35=2433^5 = 243. Missing a given letter: 25=322^5 = 32, three ways. Missing two given letters: 11 string, three ways. Missing all three: impossible.

Step 2. Strings missing at least one letter: 3⋅32−3⋅1=933 \cdot 32 - 3 \cdot 1 = 93.

Step 3. So 243−93=150243 - 93 = 150 strings use all three. This is the count of onto functions from 55 positions to 33 letters, and the same pattern handles any "every box gets something" question.

Side note
Where it goes wrong

Stopping after the pairs. For the integers 11 to 100100 divisible by 22, 33 or 55, the count 50+33+20−16−10−6=7150 + 33 + 20 - 16 - 10 - 6 = 71 is three short: the multiples of 3030 (30,60,9030, 60, 90) were added three times and then subtracted three times, so they vanished. Adding back the triple overlap gives the correct 7474.

Try it
Try it: neither 4 nor 6

How many integers from 11 to 100100 are divisible by neither 44 nor 66?

Show the answer

Multiples of 44: 2525. Multiples of 66: 1616. Multiples of both are the multiples of lcm⁡(4,6)=12\operatorname{lcm}(4, 6) = 12, not of 2424: there are 88. So 25+16−8=3325 + 16 - 8 = 33 are divisible by at least one, and 100−33=67100 - 33 = 67 by neither. (Using 2424 for the overlap gives the wrong answer 6363.)

Stars and Bars

Tip
Distributing identical objects
kid 1: 3kid 2: 5kid 3: 2
One of the C(12, 2) = 66 rows: 10 stars and 2 bars, read as the split 3 + 5 + 2.

Nonnegative solutions of x1+⋯+xk=nx_1+\cdots+x_k=n number (n+k−1k−1)\dbinom{n+k-1}{k-1}; positive solutions number (n−1k−1)\dbinom{n-1}{k-1}.

In plain terms. Line up nn identical items and k−1k-1 dividers; where the dividers go decides who gets what. Counting the arrangements is one binomial.

Example. Give 1010 candies to 33 kids: (122)=66\dbinom{12}{2} = 66 ways; if each must get one, (92)=36\dbinom{9}{2} = 36.

Why it works. A distribution of nn identical items to kk people is the same thing as a row of nn stars and k−1k - 1 bars: the stars before the first bar go to person 11, those between the first two bars to person 22, and so on. Every row gives one distribution and every distribution gives one row, so count rows: choose which k−1k - 1 of the n+k−1n + k - 1 positions hold bars. For positive amounts, hand out one each first and distribute the remaining n−kn - k.

Example
Worked: minimums first

How many solutions in nonnegative integers does a+b+c+d=12a + b + c + d = 12 have with a≥2a \ge 2 and b≥1b \ge 1?

Spotting it. Lower bounds are handled by paying them up front, which turns the problem back into a plain stars-and-bars count.

Step 1. Put a′=a−2a' = a - 2 and b′=b−1b' = b - 1. Then a′+b′+c+d=9a' + b' + c + d = 9 with all four nonnegative.

Step 2. 99 stars and 33 bars: (123)=220\dbinom{12}{3} = 220.

Example
Worked: an upper bound by complement

How many solutions in nonnegative integers does x1+x2+x3=15x_1 + x_2 + x_3 = 15 have with every xi≤7x_i \le 7?

Spotting it. Stars and bars cannot enforce a CAP directly, but it can count the violators: a variable that is at least 88 is a lower bound, handled by paying 88 up front. Then inclusion–exclusion removes them.

Step 1. Without caps: (172)=136\dbinom{17}{2} = 136.

Step 2. Solutions with x1≥8x_1 \ge 8: put x1′=x1−8x_1' = x_1 - 8, so x1′+x2+x3=7x_1' + x_2 + x_3 = 7, giving (92)=36\dbinom{9}{2} = 36. The same for x2x_2 and x3x_3.

Step 3. Two variables cannot both be 88 or more, since 16>1516 > 15, so there are no pair corrections. The answer is 136−3⋅36=28136 - 3 \cdot 36 = 28.

Side note
Where it goes wrong

Shifting by the wrong amount. "x1≤7x_1 \le 7 fails" means x1≥8x_1 \ge 8, so the shift is 88, not 77. Subtracting 3(102)=1353\dbinom{10}{2} = 135 (from a shift of 77) leaves 11 instead of 2828, and that absurd answer is the tell. Also keep the two formulas straight: (n+k−1k−1)\dbinom{n + k - 1}{k - 1} for nonnegative parts, (n−1k−1)\dbinom{n - 1}{k - 1} for positive ones.

Try it
Try it: three dice

In how many ways (order matters) can three standard dice show a total of 1010?

Show the answer

Let yi=xi−1y_i = x_i - 1, so y1+y2+y3=7y_1 + y_2 + y_3 = 7 with 0≤yi≤50 \le y_i \le 5. Without the cap: (92)=36\dbinom{9}{2} = 36. A violator has some yi≥6y_i \ge 6; shifting by 66 leaves a sum of 11, which has (32)=3\dbinom{3}{2} = 3 solutions, and there are 33 choices of ii. So 36−9=2736 - 9 = 27.

Distributions, Arrangements and Overcounting

Tip
Which formula, and why

Ordered with repetition: nkn^k. Ordered without: n!(n−k)!\tfrac{n!}{(n-k)!}. Unordered without: (nk)\dbinom nk. Unordered WITH repetition (stars and bars): (n+k−1k)\dbinom{n+k-1}{k}.

Almost every counting error is choosing the wrong row of that table. The test is two questions: does order matter, and may items repeat? "How many ways to give 1010 identical candies to 44 children" is unordered with repetition, (133)=286\dbinom{13}{3} = 286; "how many 44-digit PINs" is ordered with repetition, 10410^4.

Tip
Arrangements with repeated letters

n!n1! n2!⋯\dfrac{n!}{n_1!\,n_2!\cdots} for a word with repeated letters; divide by the arrangements of the identical items.

BANANA has 6!3! 2!=60\dfrac{6!}{3!\,2!} = 60 arrangements. Circular arrangements divide by nn instead (rotations are the same seating), giving (n−1)!(n-1)!, and if reflections also count as the same — a necklace — divide by 22 more. Every one of those divisions is the same idea: count everything, then divide by the size of the group of motions you are calling identical.

Example
Worked: candies and a minimum

How many ways can 1010 identical candies go to 44 children if each child gets at least one?

Step 1 — give one to each first; 66 remain.

Step 2 — distribute the 66 freely: stars and bars with n=4n = 4, k=6k = 6 gives (93)=84\dbinom{9}{3} = 84. The "at least one" condition is always handled first, by pre-assigning the minimum and then counting freely.

Example
Worked: a round table with a restriction

In how many ways can 66 people sit around a round table if Alice and Bob refuse to sit next to each other? (Rotations of a seating count as the same.)

Spotting it. "Not next to each other" is easiest as a complement: count all seatings, subtract those where they ARE together, and glue the pair into a block for the latter.

Step 1. All circular seatings of 66: (6−1)!=120(6 - 1)! = 120.

Step 2. Alice and Bob together: treat them as one block, so 55 units around the table give (5−1)!=24(5 - 1)! = 24 seatings, times 22 for the order inside the block: 4848.

Step 3. 120−48=72120 - 48 = 72.

Side note
Where it goes wrong

Forgetting that the groups are unlabelled. Splitting 88 people into 44 pairs is not (82)(62)(42)(22)=2520\dbinom82\dbinom62\dbinom42\dbinom22 = 2520: that counts each pairing once for every order in which its 44 pairs could have been chosen. Divide by 4!=244! = 24 to get 105105. Ask of every count: "if I relabel my choices, do I get a different outcome?" If not, divide.

Try it
Try it: a word count

How many arrangements of the letters of LEVEL are there?

Show the answer

5!2! 2!=30\dfrac{5!}{2!\,2!} = 30 — two Ls and two Es.