Math Core

Module 2.2 · Counting and Probability

Advanced inclusion-exclusion

Inclusion-exclusion is the tool for "none of these bad things happens" and "every one of these things happens." At the AIME level you use it with many conditions at once, and the trick is organizing the terms so that each one is a simple count.

The general formula

For sets A1,A2,…,AnA_1, A_2, \dots, A_n,

∣A1∪⋯∪An∣=∑∣Ai∣−∑∣Ai∩Aj∣+∑∣Ai∩Aj∩Ak∣−…|A_1 \cup \dots \cup A_n| = \sum |A_i| - \sum |A_i \cap A_j| + \sum |A_i \cap A_j \cap A_k| - \dots

Each element that lies in exactly r≥1r \ge 1 of the sets is counted (r1)−(r2)+(r3)−⋯=1\binom{r}{1} - \binom{r}{2} + \binom{r}{3} - \dots = 1 time, which is why the formula works.

In practice you almost always want the complement: the number of objects with none of the bad properties.

Inclusion-exclusion for 'none'

If NN objects are in total, and A1,…,AnA_1, \dots, A_n are the sets of objects with each bad property, then

#{none}=N−S1+S2−S3+⋯+(−1)nSn,\#\{\text{none}\} = N - S_1 + S_2 - S_3 + \dots + (-1)^n S_n,

where SkS_k is the sum of ∣Ai1∩⋯∩Aik∣|A_{i_1} \cap \dots \cap A_{i_k}| over all choices of kk of the sets.

When the problem is symmetric, every intersection of kk sets has the same size sks_k, and Sk=(nk)skS_k = \dbinom{n}{k} s_k.

Symmetry is what makes AIME problems workable. With 88 conditions you would never list 255255 intersections, but if they all look alike you only need one count per kk.

Three standard families

Derangements. A permutation of nn objects with no fixed points is a derangement. Let AiA_i be the permutations fixing object ii. Fixing kk specific objects leaves (n−k)!(n - k)! permutations, so

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

The first values are D1=0D_1 = 0, D2=1D_2 = 1, D3=2D_3 = 2, D4=9D_4 = 9, D5=44D_5 = 44, D6=265D_6 = 265, D7=1854D_7 = 1854. They also satisfy Dn=(n−1)(Dn−1+Dn−2)D_n = (n - 1)(D_{n-1} + D_{n-2}).

To count permutations with exactly jj fixed points, choose which jj are fixed and derange the rest: (nj)Dn−j\dbinom{n}{j} D_{n - j}.

Surjections. The number of functions from an mm-element set onto a kk-element set (every output used) is

∑i=0k(−1)i(ki)(k−i)m.\sum_{i=0}^{k} (-1)^i \binom{k}{i} (k - i)^m.

Here AiA_i is the set of functions that miss output ii.

Bounded stars and bars. To count solutions of x1+⋯+xk=nx_1 + \dots + x_k = n with upper bounds, let AiA_i be the solutions with xix_i too big. Shifting xix_i down turns each intersection into an ordinary stars-and-bars count.

Common mistake

Don't stop after subtracting the singles. Objects with two bad properties were subtracted twice and must be added back, and so on. If your answer is negative or suspiciously small, you probably forgot a term.

Worked example: Divisibility

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

Use ⌊1000/d⌋\left\lfloor 1000/d \right\rfloor for the multiples of dd:

1000−(500+333+200)+(166+100+66)−33=266.1000 - (500 + 333 + 200) + (166 + 100 + 66) - 33 = 266.

The pair terms use 66, 1010, 1515 and the triple term uses 3030.

Worked example: Exactly two fixed points

Six letters are placed at random into six addressed envelopes, one per envelope. The probability that exactly two letters land in their correct envelopes is mn\dfrac{m}{n} in lowest terms. Find m+nm + n.

Choose the two correct letters in (62)=15\dbinom{6}{2} = 15 ways; the other four must be deranged, in D4=9D_4 = 9 ways. The probability is 15⋅9720=135720=316\dfrac{15 \cdot 9}{720} = \dfrac{135}{720} = \dfrac{3}{16}, so m+n=19m + n = 19.

Worked example: Every worker gets a job

In how many ways can 66 different tasks be assigned to 33 workers so that every worker gets at least one task?

All assignments: 36=7293^6 = 729. Missing a given worker: 26=642^6 = 64, for each of 33 workers. Missing two given workers: 11, for each of 33 pairs. So the count is 729−3⋅64+3⋅1=540729 - 3 \cdot 64 + 3 \cdot 1 = 540.

Worked example: Couples not side by side

Four married couples sit in a row of 88 chairs in random order. The probability that no husband sits next to his wife is mn\dfrac{m}{n} in lowest terms. Find m+nm + n.

Let AiA_i be the seatings where couple ii sits together. For kk specific couples together, glue each into a block: (8−k)!(8 - k)! orders of the units, times 2k2^k for the order inside each block. So

∑k=04(−1)k(4k)2k(8−k)!=40320−40320+17280−3840+384=13824.\sum_{k=0}^{4} (-1)^k \binom{4}{k} 2^k (8 - k)! = 40320 - 40320 + 17280 - 3840 + 384 = 13824.

The probability is 1382440320=1235\dfrac{13824}{40320} = \dfrac{12}{35}, so m+n=47m + n = 47.

Tip

Before computing, write the answer as ∑(−1)k(nk)sk\sum (-1)^k \binom{n}{k} s_k and find a formula for sks_k. If you can't describe sks_k in one line, rethink what your sets AiA_i are.

Practice

Practice 1

How many integers from 11 to 999999 are divisible by none of 33, 55, 77?

Enter a number. Fractions like 3/4 and sqrt(2) are OK.

Practice 2

Six people each bring a gift to a party, and the gifts are redistributed at random so that each person gets one gift. The probability that nobody receives their own gift is mn\dfrac{m}{n} in lowest terms. Find m+nm + n.

Enter a number. Fractions like 3/4 and sqrt(2) are OK.

Practice 3

Let NN be the number of ways to place 77 distinguishable balls into 33 distinguishable boxes so that no box is empty. Find the remainder when NN is divided by 10001000.

Enter a number. Fractions like 3/4 and sqrt(2) are OK.

Practice 4

How many strings of length 55 using the letters AA, BB, CC, DD contain each of AA, BB, and CC at least once?

Enter a number. Fractions like 3/4 and sqrt(2) are OK.

Practice 5

Three couples sit at random around a round table with 66 seats. Seatings that differ only by a rotation are considered the same. The probability that no one sits next to their partner is mn\dfrac{m}{n} in lowest terms. Find m+nm + n.

Enter a number. Fractions like 3/4 and sqrt(2) are OK.

Practice 6

Find the number of ordered triples (a,b,c)(a, b, c) of positive integers with a+b+c=20a + b + c = 20 and a,b,c≤9a, b, c \le 9.

Enter a number. Fractions like 3/4 and sqrt(2) are OK.

Practice 7

How many integers from 11 to 10001000 are perfect squares, perfect cubes, or perfect fifth powers?

Enter a number. Fractions like 3/4 and sqrt(2) are OK.

Practice 8

Find the number of arrangements of the eight letters A,A,B,B,C,C,D,DA, A, B, B, C, C, D, D in a row such that no two identical letters are adjacent.

Enter a number. Fractions like 3/4 and sqrt(2) are OK.

Real contest practice