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.
The key contrast to keep straight:
- Permutations ( or ): 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
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.
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.”
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 .
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 .
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.
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 .
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).
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 ,
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.
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.
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.
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 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.
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 : .