Overview

Stars and bars is a technique to count the number of ways to distribute identical objects into distinct boxes, hence where its name comes from. We represent objects as stars and dividers between boxes as bars. Counting arrangements of stars and bars can reduce a distribution problem into a binomial coefficient.

Key Ideas

  • The number of ways to place nn identical objects into kk distinct boxes with non-negative integers is (n+k1k1)\binom{n+k-1}{k-1}.
  • For positive integer solutions (each bin gets at least 1), give each box one object first, then distribute the rest: (n1k1)\binom{n-1}{k-1}.
  • Stars and bars only applies when objects are identical and boxes are distinct. If objects are distinct, the rule of product can be used.
  • Upper-bound constraints (in other words, "each bin gets at most mm") require inclusion-exclusion with stars and bars.
  • Equations of the form x1+x2++xk=nx_1 + x_2 + \cdots + x_k = n with non-negative integer solutions are equivalent to distributing nn identical objects into kk boxes.

Core Skills

Stars and Bars

The scenario we use when we think about stars and bars is when nn stars in a row with k1k-1 bars placed among them to create kk groups. Each arrangement corresponds to exactly one way to split nn objects into kk boxes. Hence, there are n+k1n + k - 1 total symbols (stars and bars), and we choose which k1k-1 of those positions are bars:

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

Both forms are equal by symmetry as said in Binomial Theorem & Pascal's Triangle 1.

Finding Positive Integer Solutions

The number of solutions to x1+x2++xk=nx_1 + x_2 + \cdots + x_k = n with each xi0x_i \geq 0 is (n+k1k1)\binom{n+k-1}{k-1}.

Think of each xix_i as the number of stars in bin ii. Arranging nn stars and k1k-1 bars gives us the same way as this. There is also the notation of H that some people use instead of P (Permutations) and C (Combinatorics).

Lower Bounds Other than 0 or 1

In some problems, there may be a condition in which where the Stars and Bars method may be used. xiaix_i \geq a_i, substitute yi=xiai0y_i = x_i - a_i \geq 0. The equation becomes

y1+y2++yk=n(a1+a2++ak),y_1 + y_2 + \cdots + y_k = n - (a_1 + a_2 + \cdots + a_k),

and the non-negative formula applies to the yiy_i.

Upper Bound Constraints

If each ximx_i \leq m, use inclusion and exclusion. First, count all solutions, and then subtract those where at least one variable exceeds mm.

Let AiA_i = solutions where xim+1x_i \geq m+1. We substitute yi=xi(m+1)y_i = x_i - (m+1) to count Ai|A_i|, then apply the two or three-set PIE formula. For more information about PIE, read Inclusion Exclusion 1.

Worked Example

In how many ways can 8 identical books be distributed among 3 children if each child may receive zero or more books?

This is the number of non-negative integer solutions to x1+x2+x3=8x_1 + x_2 + x_3 = 8.

(8+3131)=(102)=45.\binom{8 + 3 - 1}{3 - 1} = \binom{10}{2} = 45.

If instead each child must receive at least one book,

(8131)=(72)=21.\binom{8-1}{3-1} = \binom{7}{2} = 21.

More Examples

Example 1: Counting Solutions to an Equation

How many non-negative integer solutions does x+y+z+w=10x + y + z + w = 10 have?

Four variables, sum 10: (10+4141)=(133)=286\binom{10 + 4 - 1}{4 - 1} = \binom{13}{3} = 286.

Example 2: Positive Solutions

How many ways can you write 12 as an ordered sum of 4 positive integers?

Positive solutions to x1+x2+x3+x4=12x_1 + x_2 + x_3 + x_4 = 12:

(12141)=(113)=165.\binom{12-1}{4-1} = \binom{11}{3} = 165.

Example 3: Lower Bound Shift

How many non-negative integer solutions does x+y+z=15x + y + z = 15 have with x2x \geq 2, y3y \geq 3, z0z \geq 0?

Substitute a=x20a = x - 2 \geq 0 and b=y30b = y - 3 \geq 0:

a+b+z=1523=10.a + b + z = 15 - 2 - 3 = 10.

Thus, we obtain (10+3131)=(122)=66.\binom{10 + 3 - 1}{3 - 1} = \binom{12}{2} = 66.

Example 4: Utilizing Inclusion-Exclusion (PIE)

How many ways can 10 identical balls be placed into 3 boxes with each box holding at most 4 balls?

Total without restriction: (122)=66\binom{12}{2} = 66.

Let AiA_i = solutions where box ii has 5\geq 5 balls. Substitute yi=xi5y_i = x_i - 5: the remaining sum is 105=510 - 5 = 5, giving (5+22)=21\binom{5+2}{2} = 21 solutions for each Ai|A_i|.

We consider if two boxes simultaneously hold 5\geq 5. This would require 10\geq 10 balls in those two boxes alone, leaving 0\leq 0 for the third, meaning 0 or all remaining go elsewhere. Specifically, 5+5=105+5=10 forces the third box to have 0: AiAj=1|A_i \cap A_j| = 1 for each pair.

No triple overlap (would need 15\geq 15 balls).

A1A2A3=3(21)3(1)+0=60.|A_1 \cup A_2 \cup A_3| = 3(21) - 3(1) + 0 = 60.

Solutions with each box 4\leq 4: 6660=666 - 60 = \boxed{6}.

Strategy Checklist

  • Identical vs. Distinct Objects
ObjectsBinsFormula
IdenticalDistinctStars and bars: (n+k1k1)\binom{n+k-1}{k-1}
DistinctDistinctRule of product: knk^n (with repetition)
IdenticalIdenticalInteger partitions (harder, no closed form)

In summary, always check whether the objects are truly identical before applying stars and bars.

  • If there are multiple types of identical objects, apply stars and bars once per type, and then multiply.

Common Pitfalls

  • Applying stars and bars to distinct objects. If they are different, use knk^n or permutations, not stars and bars.
  • Confusing (n+k1k1)\binom{n+k-1}{k-1} with (n+kk)\binom{n+k}{k} or (n+k1k)\binom{n+k-1}{k}. This could come from memorizing the formula by its derivation: nn stars and k1k-1 bars among n+k1n+k-1 total symbols.
  • Forgetting to subtract when doing the positive-solution shift , and thus giving one object to each of kk bins first.
  • Treating unlabeled boxes or "spaces" as labeled.

Practice Problems

StatusSourceProblem NameDifficultyTags
AMC 8Hard
Show TagsCombinatorics, Stars and Bars
AMC 8Unknown
AMC 8Medium
Show TagsDigits, Number Theory
AMC 10AMedium
Show TagsCombinatorics, Stars and Bars
AMC 12BHard
Show TagsCombinatorics, Probability
AMC 10AMedium
Show TagsCombinatorics, Dice, Probability
AMC 10BMedium
Show TagsCombinatorics, Probability, Symmetry

Module Progress:

Join the AoPS Community!

Stuck on a problem, or don't understand a module? Join the AoPS community and get help from other math contest students.