Stars and bars counts the number of solutions to in nonnegative integers. Picture identical stars in a row, and imagine inserting bars among them to split the stars into groups (some possibly empty) — each arrangement of stars and bars corresponds to exactly one solution.
The two core formulas
Nonnegative integer solutions ():
Positive integer solutions (): first give each variable 1 “for free,” which uses up of the total, then distribute the rest with the nonnegative formula:
When there's an upper bound
If some variable also has an upper bound (say ), there's no clean closed form — instead, count all nonnegative solutions, then subtract the ones where (using the substitution to turn that into another stars-and-bars count), applying inclusion-exclusion if multiple variables have bounds.
Worked example
Problem: How many ways are there to distribute 10 identical candies among 4 children, if each child must get at least 1 candy?
Solution: This is the positive-solutions case with :
Practice problems
1.How many nonnegative integer solutions are there to ?
2.How many ways can 15 identical books be distributed among 3 students so each gets at least 1?
3.How many nonnegative integer solutions are there to with ?
4.How many ways can 7 identical balls be placed into 3 distinct boxes, allowing empty boxes?