Combinations with Repetition
How many ways can you choose 3 scoops of ice cream from 5 flavors? Repeating a flavor is allowed. This question is subtly different from ordinary Combinations. The objects being selected are no longer required to be distinct. It matters because many real counting problems fit this pattern. Examples include distributing identical candies to children. Another is choosing exponents in a monomial. Filling a shopping cart with repeatable items is another. These are not about picking distinct objects. Instead, they involve picking a multiset of objects from a fixed set of types. The big picture is simple. Once we allow repetition, the counting problem transforms. It becomes an equivalent problem of placing dividers among a row of markers. This bijection is so clean and so widely reused that it has its own name: stars and bars. This article gives the precise definition of a combination with repetition. It builds the stars-and-bars bijection carefully. It proves the resulting formula rigorously. It also works through nontrivial numerical examples. We also isolate exactly which hypothesis is doing the work. Two things matter: finiteness of the number of types, and genuinely unlimited repetition. We show what breaks when that assumption is dropped. Combinations with repetition sit inside a larger classification scheme for counting problems. This scheme is The Twelvefold Way. It organizes every basic counting problem by two features. One is whether the domain and codomain elements are distinguishable. The other is whether the map is required to be injective, surjective, or unrestricted. The stars-and-bars technique developed here has another use. It also gives the standard formula for counting the number of ways to write an integer as an ordered sum. This is a stepping stone toward a harder theory. That theory is the order-independent study of Integer Partitions.
- A combination with repetition is an unordered selection from a set where each element may be chosen more than once — formally, a multiset.
- The number of size- multisets from an -element set is , read "n plus r minus 1 choose r."
- The proof is a bijection ("stars and bars") between multisets and binary strings of stars and bars, reducing the count to ordinary Combinations.
- The formula requires finite types and unlimited repetition of each type; dropping either hypothesis breaks the count.
- The same integer counts nonnegative integer solutions to , linking combinatorics to Diophantine counting.
- Key idea: unordered selection with repetition from types corresponds exactly to placing stars among bars, turning a hard counting problem into ordinary binomial counting.
Where this lives today
Combinations with repetition are the combinatorial backbone of counting monomials: the number of degree- monomials in variables (needed when expanding via the multinomial theorem, or when counting basis elements of a graded polynomial ring in algebraic geometry) is exactly . In statistics and machine learning, occupancy-style counting arises when distributing indistinguishable draws among categories, such as counting possible histograms with fixed total sample size in categorical/multinomial models. In computer science, the formula counts the number of ways to specify a multiset data structure of fixed size drawn from a fixed alphabet, relevant to hashing schemes and combinatorial generation algorithms (e.g., enumerating all such multisets in lexicographic order for exhaustive search). In cryptography and coding theory, weak compositions (nonnegative integer solutions to ) governed by this same formula appear when counting error patterns or symbol-weight distributions. In statistical mechanics, the Bose–Einstein counting of indistinguishable particles among energy levels is literally the stars-and-bars count, distinguishing it from Fermi–Dirac statistics (which forbids repetition, i.e., ordinary combinations) and Maxwell–Boltzmann statistics (which treats particles as distinguishable, i.e., counts sequences). This trichotomy is one motivating thread of The Twelvefold Way, and the unordered-sum viewpoint developed here is the natural on-ramp to the more intricate, order-independent theory of Integer Partitions.
Ask the tutor: Can you show me how the shift-substitution trick handles an upper-bound constraint like , using inclusion–exclusion together with this formula?
Let be a set with finite types, and let be an integer. A combination with repetition of size from (also called an -combination of a multiset over , or simply a multiset of size drawn from ) is a function
where records how many times element is selected. Two such selections are considered the same combination with repetition if and only if they have the same multiplicity function ; in particular, order of selection is irrelevant and no upper bound is placed on any other than . Equivalently, writing , a combination with repetition of size is an unordered list
i.e. a weakly increasing sequence of indices from .
Intuition
Ordinary combinations forbid repeats: choosing 3 people from a committee of 5 gives possibilities, and no person can be counted twice. Combinations with repetition remove that restriction. If you are filling a bag with 3 pieces of fruit chosen from apple, banana, cherry, mango, kiwi (5 types), then "apple, apple, banana" is a perfectly legal bag distinct from "apple, banana, banana." The object being counted is really a vector of nonnegative integers recording how many of each type you took, subject only to the total summing to . The key trick for counting these vectors is to encode each one as a string of symbols. Picture identical stars representing the items chosen, and imagine partitioning them into groups (one group per type) using bars . The number of stars before the first bar is , between the first and second bar is , and so on. This turns an abstract multiset-counting problem into the very concrete problem of arranging a string of stars and bars, which is a standard binomial-coefficient count.
Notice the string above has exactly stars (total scoops) and bars (separating 5 flavor groups), for a total length of symbols. Any such string determines a unique multiplicity vector, and any multiplicity vector determines a unique string: this is precisely the bijection the theorem below makes rigorous.
Let and be integers. The number of combinations with repetition of size chosen from a set of distinct types — equivalently, the number of functions with — equals
We exhibit an explicit bijection (proof technique: bijective proof) between the set
and the set of binary strings of length containing exactly copies of the symbol (star) and copies of the symbol (bar).
Construction of . Given , define the string
This string has bars, one placed after each block except the last, and its total number of stars is by hypothesis. Hence the string has length and lies in , so is well defined as a map .
is injective. Suppose are two distinct elements of . Let be the smallest index with ; WLOG . In , reading left to right, the bar that terminates block (the -th bar) is preceded by exactly stars since the start of the string, while in the -th bar is preceded by stars (using for ). Since , these counts differ, so the -th bar occupies a different position in the two strings. Hence , proving injectivity.
is surjective. Let be any string of stars and bars. The bars divide into exactly maximal runs of consecutive stars, read left to right (a run may be empty, e.g. two adjacent bars, or a bar at the very start or end, produces an empty run). Formally: let be the positions of the bars in ; set and ; define to be the number of star-symbols strictly between position and position , for (with the convention that positions are and are sentinels). Each , and since every symbol of is either one of the bars or one of the stars, and the stars are exactly partitioned among the runs, we get . Thus , and by construction . This proves surjectivity. Since is a bijection, .
Counting . A string in is determined uniquely by choosing which of its positions hold a star (the remaining positions automatically hold bars). By the definition of the binomial coefficient as the number of -element subsets of an -element set, Combinations gives
Combining the two displayed equalities, . Since is exactly the set of multiplicity vectors of size- combinations with repetition from an -element set (as in the definition above), the theorem is proved. Finally by the symmetry identity for binomial coefficients, with , .
∎For , , the number of size- combinations with repetition from types satisfies
This follows immediately from Pascal's rule applied with , since and , and by Pascal's identity, part of Binomial Coefficient Identities.
Worked examples
Example 1: A snack order with an odd number of types. A vending kiosk sells distinct snack items. A customer buys a bag of snacks, and may repeat items (e.g. 2 bags of chips and 2 candy bars). How many distinct bags are possible? By the theorem, the count is
Compute . So there are exactly distinct snack bags. Sanity check via the recurrence in the corollary: . We can compute and ; indeed , matching. This double computation confirms the arithmetic is self-consistent.
Example 2: Integer solutions with a shifted lower bound. Find the number of integer solutions to
This is a disguised combinations-with-repetition problem: substitute , , , , . The equation becomes
a sum of nonnegative integers equal to . By the theorem (with "slots," "units"), the number of nonnegative solutions — which biject exactly with the multiplicity vectors constructed in the proof — is
Compute . Since the substitution is a bijection between solutions in 's and solutions in 's (it is invertible, ), the original system also has exactly integer solutions.
The theorem assumes each type may be repeated with no cap other than the total . Drop this and let each type be usable at most once more than once is disallowed beyond a fixed cap — concretely, suppose types but each type may be selected at most 1 time (so effectively repetition is banned), while we still ask for size- selections. The stars-and-bars count would claim selections. But with the cap enforced, the only valid multiplicity vectors summing to with each entry in are — exactly , not . The bijection in the proof is still injective and its image still consists of valid star-bar strings, but three of the six strings — such as , corresponding to — decode to multiplicity vectors that violate the cap and are therefore not legitimate elements of the capped counting problem. Surjectivity onto the capped set fails: the formula overcounts whenever an artificial upper bound on multiplicities is imposed. (Counting selections with individual caps requires inclusion–exclusion or generating functions, a strictly harder problem than the uncapped case handled here.)
Scope, edge cases & common traps
| Situation | Correct formula / remark |
|---|---|
| Exactly combination (the empty selection); for any , consistent. | |
| Exactly combination for every (must pick the single type times); . | |
| Order matters, repetition allowed | Use (sequences), not . |
| Order irrelevant, repetition forbidden | Use ordinary , requires . |
| Per-type upper bounds imposed | Formula overcounts; needs inclusion–exclusion (see counterexample). |
| , | No types available, selections; formula's derivation assumed and should not be applied. |
The mathematician behind it
Origins · 1 minWilliam Feller and how this was discovered
William Feller was a Croatian-American mathematician whose towering two-volume text An Introduction to Probability Theory and Its Applications popularized the phrase "stars and bars" for this counting technique and placed combinations-with-repetition squarely within the toolkit of discrete probability. Feller's broader legacy lies in making rigorous, combinatorially-grounded probability accessible to generations of scientists — the stars-and-bars device appears there as a workhorse for counting occupancy problems (balls into boxes) that recur constantly in statistical mechanics and queueing theory.
How is a combination with repetition different from an ordinary combination?
An ordinary combination selects distinct objects from available objects, so no object can appear more than once and is required for the count to be nonzero. A combination with repetition instead allows any object to be chosen zero, one, or many times, up to the total budget of selections, and there is no requirement that . Formally, ordinary combinations count -element subsets of an -set, giving , while combinations with repetition count size- multisets drawn from an -element universe, giving . The repetition version is always at least as large as the non-repetition version whenever both are defined for the same .
Why is the formula and not something simpler like ?
It might seem tempting to "remove order" from the ordered sequences by dividing by , but this overcorrects: sequences with repeated entries have fewer than distinct orderings, so dividing every sequence's count by the same undercounts multisets that contain repeats and would not even give an integer in general. The correct approach abandons sequences entirely and instead encodes each multiset directly as a stars-and-bars string, which is inherently unordered by construction. This is why the rigorous bijective proof, rather than a naive counting shortcut, is essential here.
Does the stars-and-bars formula require ?
No, and this is one of its most important contrasts with ordinary combinations. Since types can repeat, you can select items even when far exceeds ; for instance, choosing scoops of ice cream from flavors is perfectly sensible, with count . Ordinary combinations vanish once because you cannot pick more distinct objects than exist, but multisets face no such obstruction since repetition supplies the extra volume.
How does this relate to counting integer solutions of linear equations?
Every combination with repetition of size from types corresponds exactly to a nonnegative integer solution of , as shown in the proof's set . So "count the multisets" and "count the nonnegative integer solutions to a sum-equals- equation" are literally the same problem, just described in different vocabulary. This translation is extremely useful: problems phrased as inequalities like can be shifted by substitution into the standard nonnegative form and then solved directly with , as illustrated in Worked Example 2.
What happens at the boundary cases or ?
Both boundary cases are consistent with the formula and worth checking by hand. When , there is exactly one combination — choosing nothing — and for every , matching. When , there is only one type available, so the unique multiset of size is "take that one type times," and indeed . These sanity checks are a good habit whenever applying the formula to a new problem.
Can this technique be extended to count multisets with a fixed number of distinct types actually used?
Yes, though it requires an extra layer of reasoning beyond the basic theorem. If you require that all types appear at least once (i.e., every ), first set so that , then apply the theorem to get , valid when . More refined questions — such as counting multisets using exactly distinct types out of — combine this shift trick with an additional choice of which types are used, i.e. .
How does this fit into the Twelvefold Way?
The Twelvefold Way classifies counting problems by three binary choices: whether the domain (the selections) is distinguishable, whether the codomain (the types) is distinguishable, and what property the assignment must satisfy (arbitrary, injective, or surjective). Combinations with repetition sit in the cell where selections are indistinguishable from each other, types are distinguishable, and the map is arbitrary (any type can serve any number of selections) — this cell's count is exactly . Seeing this placement clarifies why ordinary combinations, permutations, and set partitions all sit as neighboring cells governed by the same organizing logic.