Math Core

Module 2.1 · Counting and Probability

Bijections

Many AIME counting problems look messy until you find a second set of objects that is easy to count and matches the first set one-to-one. That matching is called a bijection. Once you have it, the two sets have the same size, and the hard count becomes an easy one.

What a bijection buys you

Suppose you want ∣A∣|A|, the number of objects in some awkward set AA. If you can describe a rule that turns each object of AA into an object of a nicer set BB, and a rule that turns each object of BB back into exactly one object of AA, then ∣A∣=∣B∣|A| = |B|.

Definition

Bijection

A bijection between sets AA and BB is a pairing in which every element of AA is matched with exactly one element of BB and every element of BB is matched with exactly one element of AA. If a bijection exists, then ∣A∣=∣B∣|A| = |B|.

To prove a rule is a bijection, check two things: the rule always lands in BB (it is well defined), and you can undo it (every object of BB comes from exactly one object of AA). On the AIME you rarely write this out, but you should always ask yourself, "Can I run this backwards?"

Stars and bars

The most useful bijection in contest counting turns solutions of an equation into arrangements of symbols.

A solution to x1+x2+x3=7x_1 + x_2 + x_3 = 7 in nonnegative integers, such as (2,0,5)(2, 0, 5), can be written as a row of 77 stars split by 22 bars:

⋆⋆∣  ∣⋆⋆⋆⋆⋆\star\star \mid \; \mid \star\star\star\star\star

The stars before the first bar give x1x_1, the stars between the bars give x2x_2, and the stars after the second bar give x3x_3. Every row of 77 stars and 22 bars gives exactly one solution, and every solution gives exactly one row. So the number of solutions is the number of ways to choose which 22 of the 99 positions hold bars: (92)=36\binom{9}{2} = 36.

Stars and bars

The number of solutions of x1+x2+⋯+xk=nx_1 + x_2 + \dots + x_k = n in nonnegative integers is (n+k−1k−1)\dbinom{n + k - 1}{k - 1}.

The number of solutions in positive integers is (n−1k−1)\dbinom{n - 1}{k - 1} (choose k−1k - 1 of the n−1n - 1 gaps between stars).

Lower bounds are handled by another bijection: a shift. If x1≥3x_1 \ge 3, set y1=x1−3≥0y_1 = x_1 - 3 \ge 0. The map x1↦x1−3x_1 \mapsto x_1 - 3 is a bijection between the solutions you want and solutions of a new equation with a smaller total.

Spreading out: gaps between chosen numbers

Choosing numbers with no two consecutive is another classic. If a1<a2<⋯<aka_1 < a_2 < \dots < a_k are chosen from {1,2,…,n}\{1, 2, \dots, n\} with no two consecutive, then

bi=ai−(i−1)b_i = a_i - (i - 1)

gives b1<b2<⋯<bkb_1 < b_2 < \dots < b_k chosen from {1,2,…,n−k+1}\{1, 2, \dots, n - k + 1\} with no restriction. Subtracting 0,1,2,…0, 1, 2, \dots squeezes out the forced gaps, and adding them back undoes it. So the count is (n−k+1k)\dbinom{n - k + 1}{k}.

The same trick works for any required gap: if consecutive chosen numbers must differ by at least dd, subtract (i−1)(d−1)(i - 1)(d - 1) from aia_i.

A close cousin handles non-decreasing sequences. If 1≤a1≤a2≤⋯≤ak≤n1 \le a_1 \le a_2 \le \dots \le a_k \le n, then ci=ai+(i−1)c_i = a_i + (i - 1) is strictly increasing with values in {1,…,n+k−1}\{1, \dots, n + k - 1\}, so there are (n+k−1k)\dbinom{n + k - 1}{k} such sequences.

Common mistake

Always check the range of the new objects. After bi=ai−(i−1)b_i = a_i - (i - 1), the largest possible bkb_k is n−(k−1)n - (k - 1), not nn. Getting the endpoint wrong by one is the most common error with these shifts.

Lattice paths and reflection

A path of unit steps right (RR) and up (UU) from (0,0)(0, 0) to (a,b)(a, b) is the same thing as a word with aa letters RR and bb letters UU, so there are (a+ba)\dbinom{a + b}{a} of them.

Harder questions forbid paths from crossing a line. The reflection principle counts the bad paths by a bijection: take a bad path, find the first point where it touches the forbidden line, and reflect the rest of the path across that line. Bad paths to (a,b)(a, b) match exactly with all paths to the reflected endpoint.

For paths from (0,0)(0, 0) to (n,n)(n, n) that never go above the line y=xy = x, the bad paths first touch y=x+1y = x + 1. Reflecting the rest of the path across that line sends the endpoint (n,n)(n, n) to (n−1,n+1)(n - 1, n + 1). So the good paths number

(2nn)−(2nn−1)=1n+1(2nn),\binom{2n}{n} - \binom{2n}{n - 1} = \frac{1}{n + 1}\binom{2n}{n},

the Catalan numbers 1,1,2,5,14,42,132,…1, 1, 2, 5, 14, 42, 132, \dots

A related fact, the ballot theorem: if candidate AA gets pp votes and BB gets qq votes with p>qp > q, and the votes are counted in random order, the probability that AA is strictly ahead the whole time is p−qp+q\dfrac{p - q}{p + q}.

Worked example: Stars and bars with positive parts

How many ordered triples (a,b,c)(a, b, c) of positive integers satisfy a+b+c=12a + b + c = 12?

Line up 1212 stars. There are 1111 gaps between them, and choosing 22 gaps for bars splits the stars into three nonempty groups. The answer is (112)=55\dbinom{11}{2} = 55.

Worked example: No two consecutive

In how many ways can you choose 33 numbers from {1,2,…,20}\{1, 2, \dots, 20\} so that no two chosen numbers are consecutive?

Write the chosen numbers as a1<a2<a3a_1 < a_2 < a_3 and set b1=a1b_1 = a_1, b2=a2−1b_2 = a_2 - 1, b3=a3−2b_3 = a_3 - 2. Because ai+1≥ai+2a_{i+1} \ge a_i + 2, the bib_i are strictly increasing, and they lie in {1,…,18}\{1, \dots, 18\}. Conversely, any 33 numbers from {1,…,18}\{1, \dots, 18\} give a valid choice by adding back 0,1,20, 1, 2. The answer is (183)=816\dbinom{18}{3} = 816.

Worked example: Non-decreasing sequences

How many sequences 0≤a1≤a2≤a3≤a4≤90 \le a_1 \le a_2 \le a_3 \le a_4 \le 9 of integers are there?

A non-decreasing sequence is the same as a multiset: it only records how many times each value 0,1,…,90, 1, \dots, 9 appears. Let xjx_j be the number of terms equal to jj. Then x0+x1+⋯+x9=4x_0 + x_1 + \dots + x_9 = 4 with each xj≥0x_j \ge 0, which has (4+99)=(134)=715\dbinom{4 + 9}{9} = \dbinom{13}{4} = 715 solutions.

Worked example: Paths that stay below the diagonal

How many paths of unit steps right and up go from (0,0)(0, 0) to (6,6)(6, 6) and never pass above the line y=xy = x?

All paths: (126)=924\dbinom{12}{6} = 924. A bad path touches y=x+1y = x + 1; reflecting its remainder after the first touch gives a path to (5,7)(5, 7), and every path to (5,7)(5, 7) arises this way. There are (125)=792\dbinom{12}{5} = 792 of those. So the answer is 924−792=132924 - 792 = 132, the sixth Catalan number.

Tip

When a problem says "find the number of ordered tuples with a product condition," factor into primes. Distributing the exponent of each prime among the variables is a separate stars-and-bars count, and you multiply the results.

Practice

Practice 1

Find the number of ordered quadruples (a,b,c,d)(a, b, c, d) of integers with a≥2a \ge 2, b≥0b \ge 0, c≥5c \ge 5, d≥1d \ge 1, and a+b+c+d=20a + b + c + d = 20.

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

Practice 2

Find the number of 44-element subsets of {1,2,3,…,15}\{1, 2, 3, \dots, 15\} that contain no two consecutive integers.

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

Practice 3

Find the number of ordered quadruples (a,b,c,d)(a, b, c, d) of nonnegative integers with a+b+c+d≤9a + b + c + d \le 9.

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

Practice 4

Find the number of four-digit positive integers whose digits are in non-increasing order from left to right, such as 95209520 or 77777777.

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

Practice 5

Fourteen chairs are equally spaced around a round table. In how many ways can you choose 44 of the chairs so that no two chosen chairs are next to each other?

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

Practice 6

In an election, candidate Ana receives 88 votes and candidate Ben receives 55 votes. The 1313 ballots are counted one at a time in a uniformly random order. The probability that Ana is strictly ahead of Ben after every ballot is counted is mn\dfrac{m}{n}, where mm and nn are relatively prime positive integers. Find m+nm + n.

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

Practice 7

Find the number of ordered triples (a,b,c)(a, b, c) of positive integers with abc=106abc = 10^6.

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

Practice 8

Find the number of 55-element subsets of {1,2,3,…,20}\{1, 2, 3, \dots, 20\} in which any two elements differ by at least 33.

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

Real contest practice

  • 1998 AIME, Problem 7: a stars-and-bars count after a substitution that fixes the parity of each variable.
  • 1998 AIME, Problem 13: pair each subset that contains the largest element with the subset that does not.