Module 2.2 · Counting and Probability
Stars and bars
How many ways can you hand out identical candies to kids? How many ways can dice show a total of ? How many terms does 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 with
Draw stars in a row and insert bars to cut them into groups. The number of stars before the first bar is , between the first and second bars is , and so on. For example,
means , , , . Two bars side by side mean an empty group.
Every solution gives exactly one row of stars and bars, and every such row gives exactly one solution. A row is symbols, and you only need to decide which of the positions hold bars. So there are solutions.
Stars and bars
The number of solutions to in nonnegative integers is
The number of solutions in positive integers is : choose of the spaces between stars to hold a bar.
The formula counts ways to put identical objects into distinct boxes. If the objects are different (distinct balls), stars and bars does not apply; that's instead.
Lower bounds: pre-fill the boxes
If , give box its first and write with . Substitute, and you're back to a plain nonnegative problem with a smaller total.
Upper bounds: subtract the violators
If , count all solutions, then subtract those where some (pre-fill 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 dice can sum to is a positive-integer problem with an upper bound of on each part.
- Terms of an expansion. Each term of is with .
- Nondecreasing sequences. A sequence is determined by how many times each value appears. That is stars in boxes.
- Inequalities. becomes an equation by adding a slack variable .
Worked example: Pre-filling
In how many ways can identical candies be given to children so that each child gets at least ?
Give each child candies first. That uses , leaving to be handed out with no restriction: with . The count is .
Worked example: An upper bound
How many solutions does have in nonnegative integers with each variable at most ?
All solutions: .
Solutions with : set , so , giving . The same holds for and for , so subtract .
Two variables at least : that uses all , so , is the only case. There are such pairs, and we subtracted each of these solutions twice, so add back.
The answer is . (Check by listing: the permutations of and give , plus makes .)
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 or )?
Such a number is determined by how many of each digit through it uses. That's stars in boxes: .
Common mistake
Don't mix up the two formulas. "Each box may be empty" is ; "each box gets at least one" is . 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 of positive integers satisfy ?
Write , and similarly for and . The exponents of must satisfy , which has solutions. The exponents of satisfy , which has solutions. The two choices are independent, so the answer is .
Practice
In how many ways can identical balls be placed into different boxes? (Boxes may be empty.)
Enter a number. Fractions like 3/4 and sqrt(2) are OK.
How many ordered -tuples of positive integers satisfy ?
After is expanded and like terms are combined, how many terms are there?
Enter a number. Fractions like 3/4 and sqrt(2) are OK.
How many three-digit positive integers have digits whose sum is ?
How many ordered triples of nonnegative integers satisfy ?
Enter a number. Fractions like 3/4 and sqrt(2) are OK.
How many ordered triples of positive odd integers satisfy ?
Four standard six-sided dice are rolled, and the dice are distinguishable. In how many of the outcomes is the sum of the four numbers equal to ?
How many sequences of integers satisfy ?
Enter a number. Fractions like 3/4 and sqrt(2) are OK.
How many ordered quadruples of positive integers satisfy ?
Real contest practice
- 2018 AMC 10A, Problem 11: the number of ways several dice can reach a given sum.
- 2016 AMC 10A, Problem 20: counting the terms of a multinomial expansion that contain every variable.