Skip to content
Combinatorics · Chapter 5

Stars & Bars

Share identical objects among distinct bins by lining up stars and bars, then bend the trick to handle minimums, upper limits, parity, inequalities and seating gaps.

You'll need
open on YouTube
5.10%

Stars and Bars Fundamentals

In this section you will be able to count the ways to share identical objects among different bins by lining up stars and bars.

You have 8 identical computers for 3 students. The computers are identical, so only how many each student gets matters. How do you count that?

Write the computers in a row and drop in 2 bars. Everything left of the first bar goes to student 1, everything between the bars goes to student 2, and the rest goes to student 3.

Every row of stars and bars is exactly one way to share. So you only need to choose where the bars go.

Exploreplay first, formulas later

4 computers into 3 students: C(6, 2) = 15 layouts

computers4
students3
  • student 1
    2
  • student 2
    1
  • student 3
    1
15 layouts

CC|C|C gives student 1: 2, student 2: 1, student 3: 1. 15 layouts in all.

Name it

Theorem 5.1.1 · Stars and Bars

The number of ways to place nn identical objects into kk distinguishable bins is

(n+k−1n).\binom{n+k-1}{n}.

Remark 5.1.2

Stars and Bars is very useful, and can often be adapted based on situations. For example, if each bin has to have at least 1 object in it, we assign 1 object to each bin to start off, and apply our formula to the remaining n−kn - k objects and kk distinguishable bins.

Watch itpredict, then reveal

Example 5.1

How many ways are there to distribute 8 identical computers amongst 3 students?

Stars and bars (its total is revealed at the end of the example)

computers8
students3
  • student 1
    2
  • student 2
    5
  • student 3
    1
How many layouts? Predict first.

CC|CCCCC|C gives student 1: 2, student 2: 5, student 3: 1.

  1. 1

    Keep in mind that the computers are identical. What would the answer be if they were all different?

    Predict first:

Try it

Problem 5.5.7(additional)

How many terms will be in the expansion of (w+x+y+z)10(w + x + y + z)^{10} after simplifying?

Contest cornerreal contest problems

Problem 5.5.3

AMC 10

Pat is to select six cookies from a tray containing only chocolate chip, oatmeal, and peanut butter cookies. There are at least six of each of these three kinds of cookies on the tray. How many different assortments of six cookies can be selected?

Checkpoint for section 5.1

Get 2 of 3 right to master this section. One try each, no hints.
1

In how many ways can 5 identical candies be shared among 3 children, if a child may get none?

2

How many solutions does x+y+z+w=6x + y + z + w = 6 have in non-negative integers?

3

In how many ways can 7 identical balls be put into 4 different boxes?

5.20%

Stars and Bars With Constraints

In this section you will be able to handle minimums, inequalities and upper limits by changing the problem into plain stars and bars.

7 astronauts are stranded with only 10 identical meals, and everyone must get at least 1. Plain stars and bars allows empty bins. How do you fix that?

Hand out the minimums first. Once every astronaut has 1 meal, the rest can go anywhere: that’s plain stars and bars.

Two more moves make stars and bars fit almost any sum. For a sum that is less than something, add a slack variable that takes up the difference. For an upper limit (a die shows at most 6), count everything, then subtract the layouts that break the limit.

Exploreplay first, formulas later

Give 4 meals first, 4 left for 4 astronauts: C(7, 3) = 35 ways

meals8
astronauts4
Still to hand out
  • astronaut 1 (at least 1)
    0
  • astronaut 2 (at least 1)
    0
  • astronaut 3 (at least 1)
    0
  • astronaut 4 (at least 1)
    0
First hand out 4then 4 meals go anywhere

8 meals, 4 astronauts. Press "Give one to each".

Name it

Concept 5.2.1 · Give the Minimums First

Stars and bars is extremely useful, and can often be adapted based on situations. For example, if each bin has to have at least 1 object in it, we assign each bin 1 object to start off with and apply our formula with n−kn - k objects and kk distinguishable bins:

((n−k)+k−1k−1)=(n−1k−1).\binom{(n-k)+k-1}{k-1} = \binom{n-1}{k-1}.

Watch itpredict, then reveal

Example 5.2

7 astronauts are stranded in space with only 10 identical meals left. Every astronaut must receive at least 1 meal or else, they will starve. How many ways are there to distribute meals under this constraint?

Give one first (its total is revealed at the end of the example)

meals10
astronauts7
Still to hand out
  • astronaut 1 (at least 1)
    0
  • astronaut 2 (at least 1)
    0
  • astronaut 3 (at least 1)
    0
  • astronaut 4 (at least 1)
    0
  • astronaut 5 (at least 1)
    0
  • astronaut 6 (at least 1)
    0
  • astronaut 7 (at least 1)
    0
First hand out 7then 3 meals go anywhere

10 meals, 7 astronauts. Press "Give one to each".

  1. 1

    How can we deal with the condition that every astronaut must get at least 1 meal? How many meals are left after that?

    Predict first:

Example 5.3

Modified AMC 10

For some particular value of NN, when (a+b+c+d+1)N(a + b + c + d + 1)^N is expanded and like terms are combined, the resulting expression contains exactly 1001 terms that include all four variables a,b,c,a, b, c, and dd, each to some positive power. What is NN?

Stars and bars (its total is revealed at the end of the example)

factors6
letters5
  • a
    2
  • b
    1
  • c
    1
  • d
    1
  • 1
    1

This layout is the term a2bcda^{2}bcd

210 layouts

××|×|×|×|× gives a: 2, b: 1, c: 1, d: 1, 1: 1. 210 layouts in all.

  1. 1

    Every term looks like apbqcrds⋅1ta^p b^q c^r d^s \cdot 1^t. What must p+q+r+s+tp + q + r + s + t equal?

    Predict first:
    Click a choice or type its letter (A–E).
open on YouTube

Example 5.4

In how many ways can the sum of the top faces on five regular 6-sided dice be 24?

Stars and bars (its total is revealed at the end of the example)

  • die 1
    2
    shows 4
  • die 2
    1
    shows 5
  • die 3
    1
    shows 5
  • die 4
    1
    shows 5
  • die 5
    1
    shows 5
How many layouts? Predict first.

−1−1|−1|−1|−1|−1 gives die 1: 2, die 2: 1, die 3: 1, die 4: 1, die 5: 1.

  1. 1

    Each die shows 1 to 6, so plain stars and bars won't respect the maximum of 6. How can we use stars and bars anyway? How many "minus ones" do we hand out?

    Predict first:

Example 5.5

How many ways are there to choose positive integers a,b,c,a, b, c, and dd such that a+b+c+d<13a + b + c + d < 13?

Give one first (its total is revealed at the end of the example)

ones12
variables5
Still to hand out
  • a (at least 1)
    0
  • b (at least 1)
    0
  • c (at least 1)
    0
  • d (at least 1)
    0
  • extra e (at least 0)
    0
First hand out 4then 8 ones go anywhere

12 ones, 5 variables. Press "Give the minimums first".

  1. 1

    Stars and bars needs an equation, not "<<". How do we fix that?

open on YouTube

Contest cornerreal contest problems

Problem 5.5.1

AMC

Alice has 24 apples. In how many ways can she share them with Becky and Chris so that each of the three people has at least two apples?

Problem 5.5.2

AMC 10

When 7 fair standard 6-sided dice are thrown, the probability that the sum of the numbers on the top faces is 10 can be written as

n67,\frac{n}{6^7},

where nn is a positive integer. What is nn?

Problem 5.5.8(additional)

AMC 12

The expression

(x+y+z)2006+(x−y−z)2006(x + y + z)^{2006} + (x - y - z)^{2006}

is simplified by expanding it and combining like terms. How many terms are in the simplified expression?

Problem 5.5.9(additional)

AIME

A fair die is rolled four times. The probability that each of the final three rolls is at least as large as the roll preceding it may be expressed in the form mn\frac{m}{n} where mm and nn are relatively prime positive integers. Find m+nm + n.

Checkpoint for section 5.2

Get 2 of 3 right to master this section. One try each, no hints.
1

In how many ways can 12 identical pencils be given to 4 students so that each student gets at least 2?

2

How many solutions does a+b+c=10a + b + c = 10 have in positive integers?

3

How many ordered triples (x,y,z)(x, y, z) of non-negative integers satisfy x+y+z≤5x + y + z \le 5?

5.30%

Stars and Bars but Stars Are Groups

In this section you will be able to count shares that must be even, odd or matched by making each star a group.

Three squirrels share 19 acorns. One wants an odd number, one an even number, and one an even number of at least 4. What if the stars come in pairs?

When a share must be even, count it in pairs: write it as 2y2y and share out the yy pairs. An odd share is 2x+12x + 1: hand out the 1 first, then pairs.

A group can also be a mix. Glue each banana to an apple and move the two together.

Exploreplay first, formulas later

5 candy pairs into 3 children: C(7, 2) = 21 layouts

candy pairs5
children3
  • child 1
    2
    candies 4
  • child 2
    2
    candies 4
  • child 3
    1
    candies 2
21 layouts

●●●●|●●●●|●● gives child 1: 2, child 2: 2, child 3: 1. 21 layouts in all.

Watch itpredict, then reveal

Example 5.6

Omega Learn

You have 11 apples and 5 bananas. You place them into 3 baskets. How many ways are there to do this if each basket must have more apples than bananas and at least 1 of any fruit?

Stars and bars (its total is revealed at the end of the example)

  • basket 1
    2
    bananas 2
  • basket 2
    2
    bananas 2
  • basket 3
    1
    bananas 1
How many layouts? Predict first.

ABAB|ABAB|AB gives basket 1: 2, basket 2: 2, basket 3: 1.

  1. 1

    Each basket needs more apples than bananas. What can we glue together?

open on YouTube

Example 5.7

Omega Learn Math Contest

Three squirrels have to distribute 19 identical acorns amongst themselves.

  • The first squirrel demands to have a positive odd number of acorns.
  • The second squirrel is fine with any non-negative even number of acorns.
  • The third squirrel insists he must get an even number of acorns greater than or equal to 4.

How many different ways can they distribute the acorns?

Stars and bars (its total is revealed at the end of the example)

  • squirrel 1
    2
    acorns 5
  • squirrel 2
    3
    acorns 6
  • squirrel 3
    2
    acorns 8
How many layouts? Predict first.

●●●●|●●●●●●|●●●● gives squirrel 1: 2, squirrel 2: 3, squirrel 3: 2.

  1. 1

    Write each squirrel's share with a variable that can be any non-negative integer. How?

open on YouTube

Try it

Problem 5.5.6(additional)

Omega Learn

Find the number of solutions to a+b+c+d<19a + b + c + d < 19 where a,b,c,da, b, c, d are positive integers, aa and cc must be odd, and bb and dd must be even.

Checkpoint for section 5.3

Get 2 of 3 right to master this section. One try each, no hints.
1

How many solutions does x+y+z=10x + y + z = 10 have if x,y,zx, y, z are non-negative even integers?

2

In how many ways can 9 identical coins be shared among 3 people so that everyone gets an odd number of coins?

3

How many solutions does a+b+c=13a + b + c = 13 have if aa is a positive odd integer and b,cb, c are non-negative even integers?

5.40%

Stars and Bars to Count Orderings

In this section you will be able to count line-ups where some people or objects can't be neighbors, by sharing the others into the gaps.

7 people sit in a row, and 3 of them are enemies who refuse to sit together. Instead of counting seats, count the people in the gaps between the enemies.

Mark where the enemies sit. The other people fill the gaps around them. The inner gaps need at least 1 person, and the two ends may be empty. That’s stars and bars with minimums.

Stars and bars counts the pattern. Then multiply by the number of ways to order the people inside it.

Exploreplay first, formulas later

Give 2 friendly people first, 2 left for 4 gaps: C(5, 3) = 10 ways

Still to hand out
  • w: left end (at least 0)
    0
  • x: gap 1 (at least 1)
    0
  • y: gap 2 (at least 1)
    0
  • z: right end (at least 0)
    0
First hand out 2then 2 friendly people go anywhere

4 friendly people, 4 gaps. Press "Give the minimums first".

Watch itpredict, then reveal

Example 5.8

How many ways are there for 7 people to sit in a line if 3 of them are enemies and refuse to sit next to each other?

Give one first (its total is revealed at the end of the example)

Still to hand out
  • w: left end (at least 0)
    0
  • x: gap 1 (at least 1)
    0
  • y: gap 2 (at least 1)
    0
  • z: right end (at least 0)
    0
First hand out 2then 2 friendly people go anywhere

4 friendly people, 4 gaps. Press "Give the minimums first".

  1. 1

    We'll order the people later. First: without the condition, how many ways are there to choose 3 seats for the enemies?

    Predict first:

Example 5.9

AIME

Ed has five identical green marbles, and a large supply of identical red marbles. He arranges the green marbles and some of the red ones in a row and finds that the number of marbles whose right hand neighbor is the same color as themselves is equal to the number of marbles whose right hand neighbor is the other color. An example of such an arrangement is GGRRRGGRG. Let mm be the maximum number of red marbles for which such an arrangement is possible, and let NN be the number of ways he can arrange the m+5m + 5 marbles to satisfy the requirement. Find the remainder when NN is divided by 10001000.

Give one first (its total is revealed at the end of the example)

Still to hand out
  • left end (at least 1)
    0
  • gap 1 (at least 1)
    0
  • gap 2 (at least 1)
    0
  • gap 3 (at least 1)
    0
  • gap 4 (at least 1)
    0
  • right end (at least 1)
    0
First hand out 6then 10 red marbles go anywhere

16 red marbles, 6 gaps. Press "Give one to each".

For steps 3 and 4: the red marbles and the 6 groups around the 5 greens.
  1. 1

    At most how many marbles can have a right neighbor of the other color?

    Predict first:

Example 5.10

AIME

There are two distinguishable flagpoles, and there are 19 flags, of which 10 are identical blue flags, and 9 are identical green flags. Let NN be the number of distinguishable arrangements using all of the flags in which each flagpole has at least one flag and no two green flags on either pole are adjacent. Find the remainder when NN is divided by 10001000.

An example with fewer flags: pole 1, a red separator, then pole 2 on one long pole.
  1. 1

    We'll use complementary counting: count arrangements where a pole may be empty, then subtract the ones with all flags on 1 pole. How many ways are there to put all 19 flags on 1 flagpole?

    Predict first:

Try it

Problem 5.5.11(additional)

In how many ways can 8 students and 3 teachers sit in the front row if, between every pair of teachers, there must be at least 2 students?

Contest cornerreal contest problems

Problem 5.5.10(additional)

AIME

A gardener plants three maple trees, four oaks, and five birch trees in a row. He plants them in random order, each arrangement being equally likely. Let mn\frac{m}{n} in lowest terms be the probability that no two birch trees are next to one another. Find m+nm + n.

Problem 5.5.12(additional)

AIME

Consider the paths of length 16 that go from the lower left corner to the upper right corner of an 8×88 \times 8 grid. Find the number of such paths that change direction exactly 4 times.

Checkpoint for section 5.4

Get 2 of 3 right to master this section. One try each, no hints.
1

In how many ways can 6 people sit in a row if 2 of them refuse to sit next to each other?

2

How many ways are there to choose 3 numbers from {1,2,…,10}\{1, 2, \dots, 10\} so that no two chosen numbers are consecutive?

3

In how many ways can 5 identical blue flags and 3 identical red flags hang in a row so that no two red flags are next to each other?

5.5

Practice Problems

In this section you will be able to mix every stars-and-bars move from this chapter on contest problems.

Two harder contest problems. One hides stars and bars inside a divisibility question, the other inside a count of factorizations. Each has three hints if you get stuck.

Want every problem from this chapter on one page? Open the problem set.

Contest cornerreal contest problems

Problem 5.5.4

AMC 10

A seven-digit positive integer is chosen at random. What is the probability that the number is divisible by 11, given that the sum of its digits is 61?

Problem 5.5.5

AMC 10Challenge

This problem also appears in the book as 3.3.18.

Let D(n)D(n) denote the number of ways of writing the positive integer nn as a product

n=f1⋅f2⋯fk,n = f_1 \cdot f_2 \cdots f_k,

where k≥1k \ge 1, the fif_i are integers strictly greater than 1, and the order in which the factors are listed matters (that is, two representations that differ only in the order of the factors are counted as distinct). For example, the number 6 can be written as 66, 2⋅32 \cdot 3, and 3⋅23 \cdot 2, so D(6)=3D(6) = 3. What is D(96)D(96)?