Combinations with Repetition
A combination with repetition is a selection of k items from n types where order does not matter and the same type can be chosen more than once. The number of such selections is \binom{n+k-1}{k}. The stars and bars method explains why: k stars stand for the items and n − 1 bars separate the types.
Before this lesson: Permutations and Combinations and Sum and Product Rules for Counting
The idea
A donut shop sells glazed, chocolate and jelly donuts, with plenty of each. You want a box of 5 donuts. Two boxes are the same if they hold the same number of each flavor, so order does not matter, and you can pick a flavor more than once.
An ordinary combination like \binom{3}{5} does not work here, because it assumes you pick 5 different flavors from only 3. Instead, record each box as a row of symbols: a star for each donut and a bar between flavors.
Every box gives exactly one row, and every row of 5 stars and 2 bars gives exactly one box. So counting boxes is the same as counting rows. A row has 7 positions, and choosing which 2 of them hold bars fixes the whole row, which gives \binom{7}{2} = 21 boxes.
The combinations with repetition formula
Combinations with repetition. The number of ways to choose k items from n types, with repetition allowed and order ignored, is
\binom{n+k-1}{k} = \binom{n+k-1}{n-1} = \frac{(n+k-1)!}{k!\,(n-1)!}Why it works. Each selection is a row of k stars and n − 1 bars. The row has n + k − 1 positions, and a selection is fixed once you choose which k positions are stars, or equally which n − 1 positions are bars. A selection like this is also called a multiset, and some books write the count as a multiset coefficient, \left(\!\binom{n}{k}\!\right), read “n multichoose k.”
The same count answers an equation question. Let xi be the number of items of type i. Then a selection is a solution of the equation below in nonnegative integers (0, 1, 2, …).
x_1 + x_2 + \cdots + x_n = k \quad \text{has} \quad \binom{n+k-1}{n-1} \text{ nonnegative integer solutions}If every xi must be at least 1, first give 1 to each variable. That leaves k − n to share freely, so there are \binom{k-1}{n-1} positive integer solutions.
How to use stars and bars
- Check that the items are identical within each type and that order does not matter.
- Identify n, the number of types (boxes or variables), and k, the number of items (stars).
- If some types have a minimum, give out those items first and subtract them from k.
- Count the arrangements of k stars and n − 1 bars: \binom{n+k-1}{n-1}.
- If some type has a maximum, count the selections that break it and subtract them from the total.
The four basic ways to choose k items from n types are summarized below.
| Order matters? | Repetition allowed? | Number of ways | Example with n = 3, k = 2 |
|---|---|---|---|
| Yes | Yes | n^k | 3^2 = 9 two-letter codes from A, B, C |
| Yes | No | P(n, k) = \frac{n!}{(n-k)!} | P(3, 2) = 6 ordered pairs of different letters |
| No | No | \binom{n}{k} | \binom{3}{2} = 3 pairs of different letters |
| No | Yes | \binom{n+k-1}{k} | \binom{4}{2} = 6 multisets: AA, BB, CC, AB, AC, BC |
Combinations with repetition examples
Example 1: Choosing donuts
How many different boxes of 5 donuts can be made from glazed, chocolate and jelly?
n = 3,\ k = 5
There are 3 flavors (types) and 5 donuts (stars).
n - 1 = 2
Three flavors need 2 bars between them.
5 + 2 = 7
Each box is a row of 7 symbols.
\binom{7}{2}Choose which 2 of the 7 positions hold the bars.
\binom{7}{2} = \frac{7 \cdot 6}{2 \cdot 1}Use the combination formula (see permutations and combinations).
\frac{7 \cdot 6}{2 \cdot 1} = \frac{42}{2}Multiply.
\frac{42}{2} = 21Divide.
Answer: 21 different boxes.
Example 2: Nonnegative integer solutions
How many solutions does x_1 + x_2 + x_3 + x_4 = 10 have in nonnegative integers?
n = 4,\ k = 10
The 4 variables are the types and the 10 units are the stars.
\binom{10 + 4 - 1}{4 - 1} = \binom{13}{3}Use the formula with n − 1 = 3 bars.
\binom{13}{3} = \frac{13 \cdot 12 \cdot 11}{3 \cdot 2 \cdot 1}Write the combination as a product.
\frac{13 \cdot 12 \cdot 11}{3 \cdot 2 \cdot 1} = \frac{1716}{6}Multiply the top and the bottom.
\frac{1716}{6} = 286Divide.
Answer: 286 solutions. For example, (3, 0, 5, 2) is one of them: 3 stars, a bar, no stars, a bar, 5 stars, a bar, 2 stars.
Example 3: At least one each (word problem)
A parent shares 8 identical coins among 4 children, and every child gets at least one coin. How many ways can this be done?
4 \cdot 1 = 4
Give each child 1 coin first.
8 - 4 = 4
This leaves 4 coins to share with no limits.
n = 4,\ k = 4
Now there are 4 children (types) and 4 coins (stars).
\binom{4 + 4 - 1}{4 - 1} = \binom{7}{3}Count with stars and bars.
\binom{7}{3} = \frac{7 \cdot 6 \cdot 5}{3 \cdot 2 \cdot 1}Write the combination as a product.
\frac{7 \cdot 6 \cdot 5}{3 \cdot 2 \cdot 1} = \frac{210}{6}Multiply the top and the bottom.
\frac{210}{6} = 35Divide.
Answer: 35 ways. This matches the positive-solutions formula \binom{k-1}{n-1} = \binom{7}{3}.
Example 4: Different minimums
How many nonnegative integer solutions does x_1 + x_2 + x_3 = 15 have with x_1 \ge 2 and x_2 \ge 3?
x_1 = y_1 + 2,\ x_2 = y_2 + 3,\ x_3 = y_3
Use new variables y1, y2, y3 that are each at least 0.
(y_1 + 2) + (y_2 + 3) + y_3 = 15
Substitute into the equation.
y_1 + y_2 + y_3 + 5 = 15
Combine the constants.
y_1 + y_2 + y_3 = 10
Subtract 5 from both sides.
\binom{10 + 3 - 1}{3 - 1} = \binom{12}{2}Count nonnegative solutions with stars and bars.
\binom{12}{2} = \frac{12 \cdot 11}{2 \cdot 1}Write the combination as a product.
\frac{12 \cdot 11}{2 \cdot 1} = \frac{132}{2}Multiply the top and the bottom.
\frac{132}{2} = 66Divide.
Answer: 66 solutions. Each solution in y matches exactly one solution in x, so the counts are equal.
Example 5: A maximum (word problem)
A bagel shop sells plain, sesame and everything bagels. How many different bags of 10 bagels have at most 3 everything bagels?
x_p + x_s + x_e = 10
Let each variable count the bagels of one kind.
\binom{10 + 3 - 1}{3 - 1} = \binom{12}{2}First count all bags, with no limit.
\binom{12}{2} = 66As in Example 4.
x_e \ge 4
A bag breaks the rule when it has 4 or more everything bagels.
x_p + x_s + (y_e + 4) = 10
Write xe = ye + 4 with ye at least 0.
x_p + x_s + y_e = 6
Subtract 4 from both sides.
\binom{6 + 3 - 1}{3 - 1} = \binom{8}{2}Count the bags that break the rule.
\binom{8}{2} = \frac{8 \cdot 7}{2 \cdot 1}Write the combination as a product.
\frac{8 \cdot 7}{2 \cdot 1} = \frac{56}{2}Multiply the top and the bottom.
\frac{56}{2} = 28Divide.
66 - 28 = 38
Subtract the bad bags from all bags.
Answer: 38 bags. Check by cases: with 0, 1, 2 or 3 everything bagels, the other two kinds share 10, 9, 8 or 7 bagels in 11, 10, 9 or 8 ways, and 11 + 10 + 9 + 8 = 38.
Common mistakes
| Mistake | Example of the error | Fix |
|---|---|---|
| Using an ordinary combination when repeats are allowed | 5 donuts from 3 flavors as \binom{3}{5} = 0 | Repeats are allowed, so use \binom{3 + 5 - 1}{5} = 21. |
| Swapping n and k | 3 donuts from 5 flavors solved with n = 3, k = 5 as \binom{7}{5} = 21 | n counts the types (bars + 1) and k counts the items (stars). Here n = 5, k = 3, giving \binom{7}{3} = 35. |
| Using n bars instead of n − 1 | 3 flavors and 5 donuts as \binom{8}{5} = 56 | Three groups need only 2 dividers: \binom{7}{2} = 21. |
| Forgetting to hand out the minimums first | 8 coins to 4 children, each at least 1, as \binom{11}{3} = 165 | Give 1 to each child, then share the other 4: \binom{7}{3} = 35. |
| Counting ordered lists | 5 donuts from 3 flavors as 3^5 = 243 | 3^5 counts the order the donuts were picked. A box ignores order. |
| Using stars and bars for different objects | 5 different books to 3 people as \binom{7}{2} | Stars must be identical. Each different book has 3 choices, so there are 3^5 = 243 ways. |
Related ideas
Stars and bars also counts the terms of a power of a sum. Every term of (x + y + z)^5 looks like x^a y^b z^c with a + b + c = 5, so the expansion has \binom{7}{2} = 21 terms; the binomial theorem is the case with two variables. Upper limits on several variables at once need the next lesson, the Inclusion-Exclusion Principle, which subtracts the overlaps correctly. Later, generating functions turn these same counts into coefficients of power series. For selections where items must be different, see permutations and combinations; for “at least two in one box” arguments, see the pigeonhole principle.
Practice problems
- How many ways can you choose 4 pieces of fruit from apples, bananas and pears, with plenty of each?
- How many nonnegative integer solutions does x_1 + x_2 + x_3 = 7 have?
- How many positive integer solutions does x_1 + x_2 + x_3 + x_4 = 9 have?
- How many different terms are in the expansion of (a + b + c + d)^3?
- How many ways can 12 identical balls be placed in 5 different boxes?
- How many nonnegative integer solutions does x_1 + x_2 + x_3 = 18 have with x_1 \ge 3, x_2 \ge 2 and x_3 \ge 5?
- A dozen donuts is chosen from 5 kinds, with at least one of each kind. How many dozens are possible?
- Three identical dice are rolled. How many different outcomes are there if only the numbers showing matter, not which die shows which?
- How many nonnegative integer solutions does x_1 + x_2 + x_3 = 9 have with x_1 \le 2?
- How many nonnegative integer solutions does the inequality x_1 + x_2 + x_3 \le 6 have?
Show answers
1.
n = 3,\ k = 4
Three kinds of fruit (types) and 4 pieces (stars).
\binom{4 + 3 - 1}{3 - 1} = \binom{6}{2}Use stars and bars.
\binom{6}{2} = \frac{6 \cdot 5}{2 \cdot 1}Write the combination as a product.
\frac{6 \cdot 5}{2 \cdot 1} = 15Multiply and divide: 30 ÷ 2 = 15.
Answer: 15 ways
2.
n = 3,\ k = 7
Three variables and 7 units.
\binom{7 + 3 - 1}{3 - 1} = \binom{9}{2}Use stars and bars.
\binom{9}{2} = \frac{9 \cdot 8}{2 \cdot 1}Write the combination as a product.
\frac{9 \cdot 8}{2 \cdot 1} = 36Multiply and divide: 72 ÷ 2 = 36.
Answer: 36 solutions
3.
9 - 4 = 5
Give 1 to each of the 4 variables first.
y_1 + y_2 + y_3 + y_4 = 5
The leftover 5 units are shared freely.
\binom{5 + 4 - 1}{4 - 1} = \binom{8}{3}Use stars and bars.
\binom{8}{3} = \frac{8 \cdot 7 \cdot 6}{3 \cdot 2 \cdot 1}Write the combination as a product.
\frac{8 \cdot 7 \cdot 6}{3 \cdot 2 \cdot 1} = 56Multiply and divide: 336 ÷ 6 = 56.
Answer: 56 solutions
4.
a^i b^j c^m d^r,\ i + j + m + r = 3
Each term has exponents that add to 3.
\binom{3 + 4 - 1}{4 - 1} = \binom{6}{3}Count the exponent choices with stars and bars.
\binom{6}{3} = \frac{6 \cdot 5 \cdot 4}{3 \cdot 2 \cdot 1}Write the combination as a product.
\frac{6 \cdot 5 \cdot 4}{3 \cdot 2 \cdot 1} = 20Multiply and divide: 120 ÷ 6 = 20.
Answer: 20 terms
5.
n = 5,\ k = 12
Five boxes (types) and 12 balls (stars).
\binom{12 + 5 - 1}{5 - 1} = \binom{16}{4}Use stars and bars.
\binom{16}{4} = \frac{16 \cdot 15 \cdot 14 \cdot 13}{4 \cdot 3 \cdot 2 \cdot 1}Write the combination as a product.
\frac{16 \cdot 15 \cdot 14 \cdot 13}{4 \cdot 3 \cdot 2 \cdot 1} = \frac{43680}{24}Multiply the top and the bottom.
\frac{43680}{24} = 1820Divide.
Answer: 1,820 ways
6.
3 + 2 + 5 = 10
Add the minimums.
18 - 10 = 8
Hand out the minimums; 8 units are left.
y_1 + y_2 + y_3 = 8
The new variables are each at least 0.
\binom{8 + 3 - 1}{3 - 1} = \binom{10}{2}Use stars and bars.
\binom{10}{2} = \frac{10 \cdot 9}{2 \cdot 1}Write the combination as a product.
\frac{10 \cdot 9}{2 \cdot 1} = 45Multiply and divide: 90 ÷ 2 = 45.
Answer: 45 solutions
7.
12 - 5 = 7
Put one donut of each kind in the box first.
n = 5,\ k = 7
Five kinds share the other 7 donuts.
\binom{7 + 5 - 1}{5 - 1} = \binom{11}{4}Use stars and bars.
\binom{11}{4} = \frac{11 \cdot 10 \cdot 9 \cdot 8}{4 \cdot 3 \cdot 2 \cdot 1}Write the combination as a product.
\frac{11 \cdot 10 \cdot 9 \cdot 8}{4 \cdot 3 \cdot 2 \cdot 1} = \frac{7920}{24}Multiply the top and the bottom.
\frac{7920}{24} = 330Divide.
Answer: 330 dozens
8.
n = 6,\ k = 3
Six faces (types) and 3 dice (stars).
\binom{3 + 6 - 1}{6 - 1} = \binom{8}{5}Use stars and bars.
\binom{8}{5} = \binom{8}{3}Choosing 5 bars is the same as choosing 3 stars.
\binom{8}{3} = \frac{8 \cdot 7 \cdot 6}{3 \cdot 2 \cdot 1}Write the combination as a product.
\frac{8 \cdot 7 \cdot 6}{3 \cdot 2 \cdot 1} = 56Multiply and divide: 336 ÷ 6 = 56.
Answer: 56 outcomes
9.
\binom{9 + 3 - 1}{3 - 1} = \binom{11}{2} = 55All solutions, with no limit: 11 · 10 ÷ 2 = 55.
x_1 = y_1 + 3
The bad solutions have x1 at least 3.
y_1 + x_2 + x_3 = 6
Subtract 3 from both sides.
\binom{6 + 3 - 1}{3 - 1} = \binom{8}{2} = 28Count the bad solutions: 8 · 7 ÷ 2 = 28.
55 - 28 = 27
Subtract the bad solutions from all solutions.
Answer: 27 solutions
10.
x_1 + x_2 + x_3 + s = 6
Add a slack variable s = 6 − (x1 + x2 + x3), which is at least 0.
n = 4,\ k = 6
Now there are 4 variables sharing exactly 6.
\binom{6 + 4 - 1}{4 - 1} = \binom{9}{3}Use stars and bars.
\binom{9}{3} = \frac{9 \cdot 8 \cdot 7}{3 \cdot 2 \cdot 1}Write the combination as a product.
\frac{9 \cdot 8 \cdot 7}{3 \cdot 2 \cdot 1} = 84Multiply and divide: 504 ÷ 6 = 84.
Answer: 84 solutions
Combinations with repetition FAQ
What is the formula for combinations with repetition?
Choosing k items from n types with repeats allowed and order ignored can be done in \binom{n+k-1}{k} ways. For 5 donuts from 3 flavors, that is \binom{7}{5} = 21.
What is the stars and bars method?
Stars and bars draws each selection as a row of k stars (the items) and n − 1 bars (the dividers between types). Counting the rows, by choosing where the bars go, counts the selections.
How is a combination with repetition different from a regular combination?
A regular combination picks k different items, so it uses \binom{n}{k}. With repetition, the same type can be picked again, so there are more choices: \binom{n+k-1}{k}.
How many nonnegative integer solutions does x₁ + ⋯ + xₙ = k have?
It has \binom{n+k-1}{n-1} solutions, since each solution is k stars split by n − 1 bars. If every variable must be positive, the count is \binom{k-1}{n-1}.
Summary
- A combination with repetition chooses k items from n types, with repeats allowed and order ignored.
- The count is \binom{n+k-1}{k} = \binom{n+k-1}{n-1}.
- Stars and bars: k identical stars for the items and n − 1 bars between the types.
- The equation x_1 + \cdots + x_n = k has \binom{n+k-1}{n-1} nonnegative and \binom{k-1}{n-1} positive integer solutions.
- Hand out minimums first; for a maximum, subtract the selections that break it.
Previous lesson: Pigeonhole Principle
Next lesson: Inclusion-Exclusion Principle
Back to the Discrete Math Index