Math Core

Module 2.2 · Counting and Probability

Stars and bars

How many ways can you hand out 1010 identical candies to 44 kids? How many ways can 44 dice show a total of 1212? How many terms does (a+b+c+d)6(a + b + c + d)^6 have? These look like different questions, but they are all the same question: how many ways can you split a number into an ordered list of parts? Stars and bars answers it with a single binomial coefficient.

The picture

Suppose you want nonnegative integers x1,x2,x3,x4x_1, x_2, x_3, x_4 with

x1+x2+x3+x4=10.x_1 + x_2 + x_3 + x_4 = 10.

Draw 1010 stars in a row and insert 33 bars to cut them into 44 groups. The number of stars before the first bar is x1x_1, between the first and second bars is x2x_2, and so on. For example,

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

means x1=2x_1 = 2, x2=5x_2 = 5, x3=0x_3 = 0, x4=3x_4 = 3. Two bars side by side mean an empty group.

Every solution gives exactly one row of 1010 stars and 33 bars, and every such row gives exactly one solution. A row is 1313 symbols, and you only need to decide which 33 of the 1313 positions hold bars. So there are (133)=286\dbinom{13}{3} = 286 solutions.

Stars and bars

The number of solutions to x1+x2+⋯+xk=nx_1 + x_2 + \cdots + x_k = n in nonnegative integers is

(n+k−1k−1).\binom{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 spaces between stars to hold a bar.

The formula counts ways to put nn identical objects into kk distinct boxes. If the objects are different (distinct balls), stars and bars does not apply; that's knk^n instead.

Lower bounds: pre-fill the boxes

If xi≥3x_i \ge 3, give box ii its 33 first and write xi=3+yix_i = 3 + y_i with yi≥0y_i \ge 0. Substitute, and you're back to a plain nonnegative problem with a smaller total.

Upper bounds: subtract the violators

If xi≤5x_i \le 5, count all solutions, then subtract those where some xi≥6x_i \ge 6 (pre-fill 66 into that box). If two boxes could both be over the limit, add those back. This is inclusion-exclusion, covered in the next module, and it usually only needs one or two terms.

Disguises

Stars and bars often hides behind other wording:

  • Dice sums. The number of ways kk dice can sum to nn is a positive-integer problem with an upper bound of 66 on each part.
  • Terms of an expansion. Each term of (a+b+c+d)6(a + b + c + d)^6 is aibjckdla^{i}b^{j}c^{k}d^{l} with i+j+k+l=6i + j + k + l = 6.
  • Nondecreasing sequences. A sequence 1≤a1≤a2≤⋯≤am≤N1 \le a_1 \le a_2 \le \cdots \le a_m \le N is determined by how many times each value 1,…,N1, \dots, N appears. That is mm stars in NN boxes.
  • Inequalities. x1+⋯+xk≤nx_1 + \cdots + x_k \le n becomes an equation by adding a slack variable s=n−(x1+⋯+xk)≥0s = n - (x_1 + \cdots + x_k) \ge 0.

Worked example: Pre-filling

In how many ways can 2020 identical candies be given to 44 children so that each child gets at least 33?

Give each child 33 candies first. That uses 1212, leaving 88 to be handed out with no restriction: y1+y2+y3+y4=8y_1 + y_2 + y_3 + y_4 = 8 with yi≥0y_i \ge 0. The count is (8+33)=(113)=165\dbinom{8 + 3}{3} = \dbinom{11}{3} = 165.

Worked example: An upper bound

How many solutions does x+y+z=12x + y + z = 12 have in nonnegative integers with each variable at most 55?

All solutions: (142)=91\dbinom{14}{2} = 91.

Solutions with x≥6x \ge 6: set x=6+x′x = 6 + x', so x′+y+z=6x' + y + z = 6, giving (82)=28\dbinom{8}{2} = 28. The same holds for yy and for zz, so subtract 3⋅28=843 \cdot 28 = 84.

Two variables at least 66: that uses all 1212, so x=y=6x = y = 6, z=0z = 0 is the only case. There are 33 such pairs, and we subtracted each of these solutions twice, so add 33 back.

The answer is 91−84+3=1091 - 84 + 3 = 10. (Check by listing: the permutations of (5,5,2)(5, 5, 2) and (5,4,3)(5, 4, 3) give 3+6=93 + 6 = 9, plus (4,4,4)(4, 4, 4) makes 1010.)

Worked example: Nondecreasing digits

How many four-digit positive integers have digits that never decrease from left to right and contain no zero (such as 13341334 or 22992299)?

Such a number is determined by how many of each digit 11 through 99 it uses. That's 44 stars in 99 boxes: (4+88)=(124)=495\dbinom{4 + 8}{8} = \dbinom{12}{4} = 495.

Common mistake

Don't mix up the two formulas. "Each box may be empty" is (n+k−1k−1)\dbinom{n+k-1}{k-1}; "each box gets at least one" is (n−1k−1)\dbinom{n-1}{k-1}. When in doubt, convert to the nonnegative version by pre-filling, and use only the first formula.

Worked example: Exponents of a product

How many ordered triples (a,b,c)(a, b, c) of positive integers satisfy abc=25⋅34abc = 2^5 \cdot 3^4?

Write a=2a13a2a = 2^{a_1}3^{a_2}, and similarly for bb and cc. The exponents of 22 must satisfy a1+b1+c1=5a_1 + b_1 + c_1 = 5, which has (72)=21\dbinom{7}{2} = 21 solutions. The exponents of 33 satisfy a2+b2+c2=4a_2 + b_2 + c_2 = 4, which has (62)=15\dbinom{6}{2} = 15 solutions. The two choices are independent, so the answer is 21⋅15=31521 \cdot 15 = 315.

Practice

Practice 1

In how many ways can 1212 identical balls be placed into 33 different boxes? (Boxes may be empty.)

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

Practice 2

How many ordered 55-tuples (a,b,c,d,e)(a, b, c, d, e) of positive integers satisfy a+b+c+d+e=12a + b + c + d + e = 12?

Practice 3

After (a+b+c+d)6(a + b + c + d)^6 is expanded and like terms are combined, how many terms are there?

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

Practice 4

How many three-digit positive integers have digits whose sum is 1010?

Practice 5

How many ordered triples (a,b,c)(a, b, c) of nonnegative integers satisfy 2a+b+c=102a + b + c = 10?

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

Practice 6

How many ordered triples (x,y,z)(x, y, z) of positive odd integers satisfy x+y+z=25x + y + z = 25?

Practice 7

Four standard six-sided dice are rolled, and the dice are distinguishable. In how many of the 646^4 outcomes is the sum of the four numbers equal to 1212?

Practice 8

How many sequences (a1,a2,a3,a4,a5)(a_1, a_2, a_3, a_4, a_5) of integers satisfy 1≤a1≤a2≤a3≤a4≤a5≤71 \le a_1 \le a_2 \le a_3 \le a_4 \le a_5 \le 7?

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

Practice 9

How many ordered quadruples (a,b,c,d)(a, b, c, d) of positive integers satisfy abcd=23⋅53abcd = 2^3 \cdot 5^3?

Real contest practice