Combinations with Repetition
Suppose you want to choose items from types, where each type is available in unlimited supply and order does not matter. The number of ways is
This counts multisets of size drawn from types. Unlike ordinary combinations , repeats are allowed, so a type may be chosen more than once.
Distributing identical objects into distinct boxes is the same as counting solutions of in non-negative integers, and stars-and-bars gives . Lower bounds are handled by giving each box its minimum first; upper bounds by subtracting the overshooting cases.
The key contrast to keep straight:
- Permutations ( with repeats allowed, without): order matters.
- Combinations : order does not matter, no repeats.
- Combinations with repetition : order does not matter, repeats allowed.
A shop sells kinds of donuts. You buy a box of donuts. How many different boxes are possible?
Solution. Here types and chosen, repeats allowed, order irrelevant. The count is
Using stars-and-bars for DISTINCT objects. The formula counts identical stars; distinguishable balls into boxes is .
How many nondecreasing sequences of length are there?
Solution. A nondecreasing sequence is the same as a multiset of size chosen from the values : sorting a multiset gives exactly one such sequence. So the count is .
Tip. “How many ways to pick with repetition allowed, order not mattering” and “how many nondecreasing sequences” are the same problem. Spotting one form in disguise is half the battle.
A fruit stand sells apples, bananas, and cherries. In how many ways can you buy pieces of fruit if the order does not matter and repeats are allowed?
Work it on paper before revealing the answer.
Show the answerHide the answer
Answer: Buying 4 pieces from 3 fruits. We choose a multiset of size from distinct fruit types (apple, banana, cherry), where order is irrelevant and repeats are allowed. This is a combination with repetition, counted by the stars-and-bars formula
Now . Hence there are ways.
Stars and Bars (identical items to distinct boxes)
To distribute identical items among distinct boxes (any box may get zero), draw stars and bars in a row. The bars split the stars into groups; group becomes the contents of box . Every arrangement of
gives one distribution, so the number of distributions is
For example, with boxes means box gets , box gets , box gets , box gets . There are symbol positions; choose which are bars.
In how many ways can identical candies be given to children (a child may get none)?
Solution. Distribute identical items into distinct boxes:
Careful. Stars and bars requires the items to be identical and the boxes to be distinct. If the objects are distinguishable, this formula does not apply — see “Distributing Distinct Objects.”
An ice-cream shop has flavors. How many ways can you order a cup of scoops if repeated flavors are allowed and order does not matter?
Work it on paper before revealing the answer.
Show the answerHide the answer
Answer: 3 scoops from 6 flavors. We pick a multiset of size from flavors (order ignored, repeats allowed):
So there are possible cups.
Nonnegative vs. Positive Integer Solutions
The number of nonnegative integer solutions to
is . This is stars and bars: each is the number of stars in box .
For positive solutions (), first give each variable unit. Substitute ; then , so the count is
How many solutions does have (a) in nonnegative integers, (b) in positive integers?
Solution. (a) Nonnegative: .
(b) Positive: set , giving , so .
Using the nonnegative formula for a "at least one each" question. Ten identical candies to children, everyone getting at least one: counts distributions where some child gets nothing. Hand out the guaranteed candies first; the remaining go anywhere, . Check the smallest case to be sure: candies, children, each at least one, is way, and .
Substitution is your workhorse. A lower bound is removed by the substitution . This turns any constant floor into the standard nonnegative problem. Just remember to subtract from the right-hand side.
Count nonnegative solutions to with , .
Solution. Let , . Then with all , giving .
How many solutions does have in positive integers?
Work it on paper before revealing the answer.
Show the answerHide the answer
Answer: Give each variable its required first: with and so on, in nonnegative integers, which is . Equivalently, place bars in the gaps between stars.
Bounded Variables (upper limits)
Lower bounds are easy (substitute). Upper bounds like need inclusion–exclusion. To count solutions of with and some cap : subtract the “bad” cases where a variable exceeds its cap.
If a variable is capped at , the bad cases have ; substitute to force that excess and count, then alternate signs over all subsets of violated caps.
How many nonnegative solutions does have with ?
Solution. Total (no cap): . Bad cases have : set , so , giving . Answer .
Count nonnegative solutions of with and .
Solution. Let be solutions with , with .
By inclusion–exclusion the good count is .
Sign pattern. Add the unrestricted total, subtract each single-cap violation, add back each double violation, subtract triples, and so on. Any term whose forced excess exceeds contributes and can be dropped.
How many solutions does have in nonnegative integers with each of at most ?
Work it on paper before revealing the answer.
Show the answerHide the answer
Answer: Without caps: . Solutions with : put , so , ; the same for and for . Two variables cannot both reach , since , so nothing is added back:
Distributing Distinct Objects
If distinct objects are placed into distinct boxes and each object is chosen independently, there are
ways (each object picks its box). This is a different world from stars and bars: here objects carry identity, so labeling matters.
Grouping distinct objects into unlabeled nonempty groups is counted by Stirling numbers of the second kind ; distributing into labeled boxes with all nonempty is (a surjection count).
In how many ways can different books be placed on distinct shelves (order on a shelf ignored, empty shelves allowed)?
Solution. Each book independently chooses one of shelves: .
Contrast: identical books on shelves would be .
Using stars and bars for distinct objects. Five different books on shelves is , not . Stars and bars only records how many books each shelf gets; it cannot tell "algebra on the top shelf, poetry below" from the reverse, and with distinct books those are different outcomes.
Decision rule. Ask: are the items identical or distinct? Identical stars and bars, . Distinct with free choice . Distinct with a fixed size in each box multinomial coefficient (next section).
In how many ways can different letters be put into different mailboxes (a mailbox may stay empty)?
Work it on paper before revealing the answer.
Show the answerHide the answer
Answer: Each letter independently chooses one of mailboxes, so . (Stars and bars would give , the number of ways to choose how many letters each box gets, which ignores which letters they are.)
Multinomial Coefficients
The number of ways to partition distinct objects into labeled groups of sizes (with ) is the multinomial coefficient
These coefficients appear in the multinomial theorem:
In how many ways can distinct cards be split into three labeled hands of sizes , , and ?
Solution.
Find the coefficient of in .
Solution. With and exponents ,
Forgetting that equal-size groups may be unlabeled. Nine players split into three teams of : if the teams are Red, Blue, Green, the count is . If the teams have no names, each split was counted once for each of the ways to paint the colours on, so the answer is .
Equal group sizes. If several groups have the same size and the groups themselves are unlabeled, divide by the factorial of the number of equal-size groups to remove overcounting. Labeled groups need no such correction.
In how many ways can players be split into three teams of named Red, Blue and Green?
Work it on paper before revealing the answer.
Show the answerHide the answer
Answer: Choose Red's players, then Blue's from the rest, then Green gets the last three:
Going Deeper: Techniques That Combine
Contest problems rarely use one idea in isolation. The strongest solvers layer them:
- Lower + upper bounds together. First substitute away lower bounds (), then apply inclusion–exclusion on the remaining upper caps. Order matters: clear floors first, then handle ceilings.
- Bounded variables via inclusion–exclusion. For with each , the good count is @@BLOCK0@@ dropping any term with a negative top argument.
- Distributing distinct objects with restrictions. “No box empty” becomes a surjection count by inclusion–exclusion on empty boxes.
- Multinomial expansions. The number of terms in before combining is ; the number of distinct monomials after combining is — stars and bars on the exponents.
Count nonnegative solutions of with each .
Solution. Here , , so :
(The term needs , so it vanishes.)
In how many ways can distinct prizes be given to students so that every student gets at least one prize?
Solution. Inclusion–exclusion on empty students:
Big picture. Every distribution problem is answered by three questions: (1) Are the items identical or distinct? (2) Are the boxes distinct, and can they be empty? (3) Are there bounds on how much a box holds? Identical items give binomial (stars-and-bars) counts; distinct items give powers, multinomials, or Stirling/surjection counts; bounds are cleared by substitution (lower) and inclusion–exclusion (upper). Name the three answers before you compute, and the right formula chooses itself.
How many ways are there to place identical balls into distinct boxes?
Work it on paper before revealing the answer.
Show the answerHide the answer
Answer: 12 identical balls into 4 distinct boxes. Nonnegative solutions of ; use stars and bars:
Therefore ways.
Compositions vs. Partitions
A composition of is an ordered sequence of positive parts summing to ; a partition is the same but order is ignored. Thus gives two compositions but only one partition ().
Compositions are just positive stars and bars. The number of compositions of into exactly positive parts is
since we place dividers into the gaps between dots. Summing over ,
compositions in total: each of the internal gaps is independently a “cut” or “no cut.”
Partitions are far subtler — there is no simple closed formula. The number of partitions of is written , and it grows fast: .
List the compositions and partitions of into parts, and reconcile the counts.
Solution. Total compositions of : . Compositions into exactly parts: , namely
By contrast the partitions of are , so — much smaller than , because reorderings collapse.
Order test. If rearranging the parts gives a different outcome, you are counting compositions — use stars and bars, or . If rearrangements are the same outcome, you are counting partitions, which need generating functions or Ferrers-diagram arguments, not a single binomial.
How many compositions of into exactly positive parts are there? How many partitions of into exactly parts?
Work it on paper before revealing the answer.
Show the answerHide the answer
Answer: Compositions: choose of the gaps between dots, . Partitions ignore order, so list them largest part first: , , , , which is . (The compositions are these four rearranged: .)
Integer Partitions and Ferrers Diagrams
A partition of is drawn as a Ferrers diagram: left-justified rows of dots, row having dots. Reflecting the diagram across its main diagonal (swap rows and columns) gives the conjugate partition , another partition of the same .
Conjugation is a bijection on partitions of , so it proves facts by symmetry. For instance: the number of partitions of into at most parts equals the number of partitions of into parts of size at most (conjugation swaps “number of rows” with “length of longest row”).
For of , the diagram and its conjugate are
Reading the column heights of the left diagram () gives exactly the rows of the right one.
Show, for , that the number of partitions into distinct parts equals the number into odd parts.
Solution. Distinct-part partitions of : — there are . Odd-part partitions of : — also .
Why (the bijection). Write each odd-part partition by grouping equal parts: a part appearing times contributes . Expand the multiplicity in binary, , and send it to the distinct parts . Every distinct part factors uniquely as (odd)(power of ), so the map reverses — proving the two counts are always equal, matching Euler's generating-function identity
Bijections beat formulas. Since has no elementary closed form, partition identities are proved by matching objects: conjugate diagrams (rows columns) or the odd/binary trick above. When a problem claims two partition counts are equal, look for a reversible construction rather than a formula.
The Generating-Function View of Stars and Bars
Each box contributes a factor recording what it can hold; multiplying the factors and reading off the coefficient of counts the distributions summing to . An unrestricted box (any count ) contributes , so free boxes give
— the stars-and-bars formula, now as a coefficient. Restrictions just change the factor:
- box must be nonempty: ;
- box holds at most : ;
- box holds only even amounts: ;
- coin of value , any number used: .
The whole distribution problem becomes: extract from a product of such factors.
In how many ways can you make cents using pennies (), nickels (), and unlimited -cent pieces?
Solution. The generating function is
and we want . Since we need only up to , truncate each factor. A nickel is used or time (). With no nickel we need of , i.e. penniestwos summing to : the twos number , giving ways. With one nickel we need of the same, i.e. sum from pennies and twos: only way. Total .
Use generating functions to count nonnegative solutions of with each .
Solution. Each capped box contributes , so we need
Only the and terms reach degree :
This is exactly the inclusion–exclusion answer — the factor is the alternating sum.
One factor per box. Build the generating function box by box: unrestricted , capped , step-. The binomial series turns coefficient extraction back into stars and bars, and the numerator polynomials reproduce inclusion–exclusion automatically.
The Twelvefold Way
Placing balls into boxes has variants: balls identical or distinct, boxes identical or distinct, and the map unrestricted / injective (“at most one per box”) / surjective (“no box empty”). Every counting formula on this sheet is one cell of the table. Below, is a Stirling number of the second kind and is the number of partitions of into at most parts.
Here is the falling factorial and is if true, else .
The last row follows from the definition of as partitions into at most parts: identical balls in identical boxes, empty boxes allowed, is a partition of into at most nonzero parts, so it is itself. With no box empty, first put one ball in each box; the other balls then form a partition into at most parts, which is .
The four rows are exactly the four big ideas of this unit: distinct balls into distinct boxes (powers, falling factorials, surjections), identical balls into distinct boxes (stars and bars, positive stars and bars), distinct balls into identical boxes (Stirling numbers), and identical balls into identical boxes (integer partitions).
Distribute balls into boxes; compare the counts for balls across the four ball/box types (unrestricted maps).
Solution.
The same physical setup gives four wildly different answers — which is why naming the ball/box types first is essential.
How many ways can identical marbles be split among identical bags with each bag nonempty?
Solution. Identical balls, identical boxes, surjective partitions of into exactly positive parts. Subtract from each part: partitions of into at most parts, i.e. . The partitions of into parts are
which is . (Contrast distinct bags: that would be the positive stars-and-bars count .)
Master decision procedure. Fix three switches before computing: (1) balls identical or distinct? (2) boxes distinct or identical? (3) map unrestricted, injective, or surjective? Distinct boxes closed-form binomials/powers; identical boxes Stirling numbers (distinct balls) or integer partitions (identical balls), which have no simple closed form. The Twelvefold Way is not twelve formulas to memorize — it is one triple of questions that routes you to the right tool on this sheet.
Why It Works
Write stars in a row and insert bars to cut them into groups; the sizes of the groups are the . Every arrangement of stars and bars corresponds to exactly one solution and vice versa, and there are ways to choose the bar positions among the symbols. For positive solutions, place the bars in the gaps between stars instead: .
Do not read a proof the way you read a rule. Cover the last line, ask what the previous line forces, and only then uncover it. If a step feels like magic, that is the step to work by hand with a concrete number.
Formulas, Proofs & Tips
What it means. Counts the ways to split identical items among distinct groups.
Example. with has solutions.
Why it works. Lay out stars and insert bars to cut them into groups. Every arrangement of symbols corresponds to exactly one solution, and choosing where the bars go is .
Tip. For , hand each group one item first, then distribute the remaining : .