Ma · Mathematics

How Many Combinations Are Possible with Limited Repetitions?

Published By the 9to5Science editorial team

At a glance

  1. 1. Set the capacities

    Record the maximum number allowed from each of the n types.

  2. 2. Model the counts

    Represent a selection by nonnegative counts whose sum is k.

  3. 3. Build the generating function

    For each type, include one term for every allowed count.

  4. 4. Extract the coefficient

    The coefficient of x to the power k gives the number of combinations.

Suppose you have nn types of items and want to select kk items, with order ignored. Type ii may be selected no more than aia_i times. The selection is determined by how many items of each type you take, so the task is to count the possible count-vectors (x1,…,xn)(x_1,\ldots,x_n) satisfying the limits and total.

The general answer is a coefficient in a generating function. This handles different limits for different types, while a shorter inclusion-exclusion formula applies when every type has the same limit.

Model a combination as counts

Let xix_i be the number selected from type ii. Then a valid combination corresponds to one integer solution of

x1+x2+⋯+xn=k,0≤xi≤ai.x_1+x_2+\cdots+x_n=k,\qquad 0\le x_i\le a_i.

Because order does not matter, selecting two items of type A and one of type B is one combination, not three different arrangements. If each physical item is distinct, or if order matters, this count-vector model is not the right model without additional choices.

A necessary condition for any selection to exist is k≤a1+⋯+ank\le a_1+\cdots+a_n. If this fails, the answer is zero.

Use a generating function for arbitrary limits

For type ii, the allowed counts are 0,1,…,ai0,1,\ldots,a_i. Encode them by the polynomial

1+x+x2+⋯+xai.1+x+x^2+\cdots+x^{a_i}.

Multiply these polynomials across all types. Choosing a term xxix^{x_i} from factor ii records taking xix_i items of that type. The exponent in the product is the total number selected, so the coefficient of xkx^k counts exactly the valid combinations:

[xk]∏i=1n(1+x+x2+⋯+xai).\boxed{[x^k]\prod_{i=1}^{n}(1+x+x^2+\cdots+x^{a_i})}.

Here [xk]F(x)[x^k]F(x) means “the coefficient of xkx^k in F(x)F(x).” This is often the cleanest answer when capacities differ. For a numerical calculation, multiply the polynomials while discarding terms of degree greater than kk.

Worked example: four types, at most two of each

How many ways can you select five items from four types if each type may appear at most twice? The generating function is

(1+x+x2)4,(1+x+x^2)^4,

so the answer is the coefficient of x5x^5.

A direct count by the pattern of selected counts is useful as a check. The nonnegative counts must sum to 5, each at most 2. The only possible patterns are (2,2,1,0)(2,2,1,0) and (2,1,1,1)(2,1,1,1), up to rearrangement.

For (2,2,1,0)(2,2,1,0), choose which type appears once in 44 ways, then choose which of the remaining three types is absent in 33 ways. The other two types appear twice. This gives 4⋅3=124\cdot3=12 combinations. For (2,1,1,1)(2,1,1,1), choose the type that appears twice in 44 ways. The total is

12+4=16.12+4=16.

Thus there are 16 combinations.

A formula when every type has the same limit

If all nn types can be chosen at most rr times, start with the unrestricted number of nonnegative solutions to x1+⋯+xn=kx_1+\cdots+x_n=k. By stars and bars, that number is (k+n−1n−1)\binom{k+n-1}{n-1}. Then use inclusion-exclusion to remove solutions in which one or more counts are at least r+1r+1:

∑j=0⌊k/(r+1)⌋(−1)j(nj)(k−j(r+1)+n−1n−1).\boxed{\sum_{j=0}^{\lfloor k/(r+1)\rfloor}(-1)^j\binom{n}{j}\binom{k-j(r+1)+n-1}{n-1}}.

A binomial coefficient term is included only for feasible indices; terms with j>nj>n are omitted. Each chosen set of jj types is shifted down by r+1r+1 selections, which explains the reduced total inside the second binomial coefficient.

For the example above, n=4n=4, r=2r=2, and k=5k=5. The formula gives

(83)−(41)(53)=56−40=16.\binom{8}{3}-\binom{4}{1}\binom{5}{3}=56-40=16.

No two types can both exceed the limit, since that would require at least six selections, so there are no further nonzero terms.

Common mistakes

  • Using ordinary combinations without repetition: (nk)\binom{n}{k} counts choosing kk distinct types, so it only applies when each type can be used at most once.
  • Using unrestricted stars and bars: (k+n−1n−1)\binom{k+n-1}{n-1} allows a type to be selected any number of times. It overcounts when there are upper limits.
  • Counting arrangements instead of combinations: If order is irrelevant, do not multiply by the number of ways to arrange the selected items.
  • Ignoring feasibility: If kk exceeds the sum of the capacities, the count is zero. If kk is small enough that no limit can be exceeded, the unrestricted stars-and-bars count is valid.

If each type must be selected at least once as well as at most aia_i times, replace its factor by x+x2+⋯+xaix+x^2+\cdots+x^{a_i}, provided every ai≥1a_i\ge1. The coefficient method then counts selections satisfying both bounds.

References

  1. Enumerative Combinatorics, Volume 1, Second Edition — Richard P. Stanley

Spotted an error? Send a correction. Read how we write and check articles in our editorial policy.