MatheramaMatherama
← Combinatorics
Combinatorics·Section 7·12 min read

Combinations with Repetition and Integral Equations

In Section 06, we counted ordered arrangements when repetition is allowed any number of times (nkn^k). Now we ask the unordered counterpart: in how many ways can we select nn objects from kk distinct types when each type is available in unlimited supply and only the quantity of each type matters?

This single idea—known as the Binary String or Stars and Bars method—translates directly into counting integer solutions of linear equations, unlocking a vast class of combinatorial problems.


Non-Negative Integer Solutions: The Fruit Shop and Binary Strings§

Let A={∞⋅a1,  ∞⋅a2,  …,  ∞⋅ak}A = \{\infty \cdot a_1,\; \infty \cdot a_2,\; \dots,\; \infty \cdot a_k\} be a multiset of kk distinct types of objects in unlimited supply, and suppose we wish to select nn objects from AA (without regard to order).

To see what "infinite supply" really means in practice, picture a fruit shop selling mangoes, oranges, and avocados (k=3k = 3 types). Suppose the shop stocks 100100 mangoes, 2525 oranges, and 5050 avocados, and you want to buy n=20n = 20 fruits in total. Even if you decide to buy all 2020 oranges, the shop has 2525 in stock and can easily fulfill your order. Whenever the stock of every type is at least nn, "sufficient supply" behaves identically to "infinite supply"!

Because the order in which fruits go into your shopping bag does not matter, a purchase is completely determined by the three non-negative integers:

  • x1=x_1 = number of mangoes bought,
  • x2=x_2 = number of oranges bought,
  • x3=x_3 = number of avocados bought,

satisfying the linear equation:

x1+x2+x3=20,x1≥0,  x2≥0,  x3≥0x_1 + x_2 + x_3 = 20, \qquad x_1 \ge 0,\; x_2 \ge 0,\; x_3 \ge 0

(Notice that xi≤20x_i \le 20 holds automatically since all variables are non-negative and sum to 2020; we do not need to write it separately.)

In general, selecting nn objects from kk types in unlimited supply is in exact bijection with finding all non-negative integer solutions (x1,x2,…,xk)(x_1, x_2, \dots, x_k) to:

x1+x2+⋯+xk=n,xi≥0(1≤i≤k)x_1 + x_2 + \dots + x_k = n, \qquad x_i \ge 0 \quad (1 \le i \le k)

The Binary String Bijection§

How do we count all non-negative integer solutions of x1+x2+x3=20x_1 + x_2 + x_3 = 20 without listing them? Construct a bijection with binary strings containing 2020 zeros (0) and 22 ones (1)!

Look at how any string of twenty 0s and two 1s encodes a unique fruit purchase:

  • x1=x_1 = number of 0s to the left of the first 1,
  • x2=x_2 = number of 0s between the two 1s,
  • x3=x_3 = number of 0s to the right of the second 1.

The two 1s act as dividers slicing the twenty 0s into three parts. Check the boundary cases carefully:

  • If the first 1 sits at the very left end, there are no 0s before it, so x1=0x_1 = 0.
  • If the two 1s sit right next to each other, there are no 0s between them, so x2=0x_2 = 0.
  • If the second 1 sits at the very right end, there are no 0s after it, so x3=0x_3 = 0.

Bijection Between Binary Strings and Non-Negative Solutions of x1 + x2 + x3 = 20

Every binary string of 2020 zeros and 22 ones corresponds to one and only one non-negative solution of x1+x2+x3=20x_1 + x_2 + x_3 = 20, and every solution corresponds to one and only one binary string. By the Bijection Principle and the Multiset Permutation Formula (Section 06), the total number of ways to buy 2020 fruits is the number of distinct arrangements of twenty 0s and two 1s:

(20+2)!20! 2!=22!20! 2!=22×212=231\frac{(20 + 2)!}{20!\,2!} = \frac{22!}{20!\,2!} = \frac{22 \times 21}{2} = 231

The General Formula§

In general, for x1+x2+⋯+xk=nx_1 + x_2 + \dots + x_k = n (xi≥0x_i \ge 0), we use a binary string of nn zeros and k−1k - 1 ones (k−1k - 1 dividers partition a row of zeros into kk regions). The total string length is n+k−1n + k - 1.

Theorem (Combinations with Unlimited Repetition): The number of unordered selections of nn objects from kk distinct types in unlimited supply—equivalently, the number of non-negative integer solutions to x1+x2+⋯+xk=nx_1 + x_2 + \dots + x_k = n—is:

(n+k−1)!n! (k−1)!=(n+k−1k−1)=(n+k−1n)\frac{(n + k - 1)!}{n!\,(k - 1)!} = \binom{n + k - 1}{k - 1} = \binom{n + k - 1}{n}

Example 1: How many distinct terms appear in the expansion of (a+b+c+d)10(a + b + c + d)^{10} after combining like terms?

Solution: Every term in the expansion has the form ax1bx2cx3dx4a^{x_1} b^{x_2} c^{x_3} d^{x_4} where x1+x2+x3+x4=10x_1 + x_2 + x_3 + x_4 = 10 and xi≥0x_i \ge 0. Here n=10n = 10 and k=4k = 4, so the number of distinct terms is:

(10+4−14−1)=(133)=13×12×116=286\binom{10 + 4 - 1}{4 - 1} = \binom{13}{3} = \frac{13 \times 12 \times 11}{6} = 286

Positive Integer Solutions (At Least One of Each Type)§

Return to the fruit shop: suppose you still want to buy 2020 fruits, but now you must buy at least one mango, at least one orange, and at least one avocado. Our equation becomes:

x1+x2+x3=20,x1≥1,  x2≥1,  x3≥1x_1 + x_2 + x_3 = 20, \qquad x_1 \ge 1,\; x_2 \ge 1,\; x_3 \ge 1

How do we reduce this to the standard form we already solved? Make a change of variables! Define:

x1=y1+1,x2=y2+1,x3=y3+1x_1 = y_1 + 1, \qquad x_2 = y_2 + 1, \qquad x_3 = y_3 + 1

Why does this substitution work so cleanly? Because x1≥1  ⟺  y1+1≥1  ⟺  y1≥0x_1 \ge 1 \iff y_1 + 1 \ge 1 \iff y_1 \ge 0 (and similarly y2≥0,y3≥0y_2 \ge 0, y_3 \ge 0). Substituting into the equation gives:

(y1+1)+(y2+1)+(y3+1)=20  ⟹  y1+y2+y3=17(y1,y2,y3≥0)(y_1 + 1) + (y_2 + 1) + (y_3 + 1) = 20 \implies y_1 + y_2 + y_3 = 17 \qquad (y_1, y_2, y_3 \ge 0)

(Intuitively, you put 11 mango, 11 orange, and 11 avocado into your basket first, and now you freely choose the remaining 1717 fruits with no restrictions!) Applying our standard formula with n=17n = 17 and k=3k = 3:

(17+3−13−1)=(192)=19×182=171\binom{17 + 3 - 1}{3 - 1} = \binom{19}{2} = \frac{19 \times 18}{2} = 171

Theorem (Positive Integer Solutions): For n≥kn \ge k, the number of positive integer solutions to x1+x2+⋯+xk=nx_1 + x_2 + \dots + x_k = n (xi≥1x_i \ge 1 for all 1≤i≤k1 \le i \le k) is:

(n−1k−1)\binom{n - 1}{k - 1}

We prove this in two illuminating ways—both ideas are worth mastering.

Proof 1 (Variable Shift): Substitute xi=yi+1x_i = y_i + 1 for each i=1,2,…,ki = 1, 2, \dots, k. Then each yi≥0y_i \ge 0 and y1+y2+⋯+yk=n−ky_1 + y_2 + \dots + y_k = n - k. By the non-negative solution formula, the number of solutions is:

((n−k)+k−1k−1)=(n−1k−1)■\binom{(n - k) + k - 1}{k - 1} = \binom{n - 1}{k - 1} \qquad \blacksquare

Proof 2 (Gap Method on Binary Strings): Consider again a binary string of nn zeros and k−1k - 1 ones. Because every variable must satisfy xi≥1x_i \ge 1, no two 1s may be adjacent (which would force a middle variable to be 00), and no 1 may sit at the leftmost or rightmost end (which would force x1=0x_1 = 0 or xk=0x_k = 0).

Lay out the nn zeros in a line. Between consecutive zeros there are exactly n−1n - 1 interior gaps. Placing the k−1k - 1 identical ones into k−1k - 1 of these n−1n - 1 interior gaps automatically guarantees at least one zero in every region! Choosing k−1k - 1 gaps out of n−1n - 1 can be done in (n−1k−1)\binom{n - 1}{k - 1} ways. ■\blacksquare

Positive Integer Solutions via Choosing Interior Gaps Between Zeros


General Lower Bounds and Linear Inequalities§

The variable-shift method handles any lower bounds xi≥aix_i \ge a_i—even negative integers!

Theorem (Arbitrary Lower Bounds): The number of integer solutions to x1+x2+⋯+xk=nx_1 + x_2 + \dots + x_k = n subject to xi≥aix_i \ge a_i for 1≤i≤k1 \le i \le k (where N=n−(a1+a2+⋯+ak)≥0N = n - (a_1 + a_2 + \dots + a_k) \ge 0) is:

(N+k−1k−1)\binom{N + k - 1}{k - 1}

Proof: Set xi=yi+aix_i = y_i + a_i. Then xi≥ai  ⟺  yi≥0x_i \ge a_i \iff y_i \ge 0, and the equation becomes y1+y2+⋯+yk=n−∑i=1kai=Ny_1 + y_2 + \dots + y_k = n - \sum_{i=1}^k a_i = N, which has (N+k−1k−1)\binom{N + k - 1}{k - 1} non-negative integer solutions. ■\blacksquare

Example 2 (Fruit Shop with Minimum Quotas): You want to buy 2020 fruits from mangoes, oranges, and avocados, subject to buying at least 22 mangoes, at least 44 oranges, and at least 55 avocados. In how many ways can you do this?

Solution: We need x1+x2+x3=20x_1 + x_2 + x_3 = 20 with x1≥2x_1 \ge 2, x2≥4x_2 \ge 4, and x3≥5x_3 \ge 5. Set x1=y1+2x_1 = y_1 + 2, x2=y2+4x_2 = y_2 + 4, x3=y3+5x_3 = y_3 + 5. Then y1,y2,y3≥0y_1, y_2, y_3 \ge 0 and:

y1+y2+y3=20−(2+4+5)=9y_1 + y_2 + y_3 = 20 - (2 + 4 + 5) = 9

With N=9N = 9 and k=3k = 3, the number of ways is:

(9+3−13−1)=(112)=11×102=55\binom{9 + 3 - 1}{3 - 1} = \binom{11}{2} = \frac{11 \times 10}{2} = 55

Example 3 (Negative Lower Bounds): Find the number of integer solutions to x1+x2+x3=10x_1 + x_2 + x_3 = 10 with x1≥−2x_1 \ge -2, x2≥0x_2 \ge 0, and x3≥1x_3 \ge 1.

Solution: Set x1=y1−2x_1 = y_1 - 2, x2=y2x_2 = y_2, and x3=y3+1x_3 = y_3 + 1, so y1,y2,y3≥0y_1, y_2, y_3 \ge 0 and:

(y1−2)+y2+(y3+1)=10  ⟹  y1+y2+y3=11(y_1 - 2) + y_2 + (y_3 + 1) = 10 \implies y_1 + y_2 + y_3 = 11

The number of solutions is (11+3−13−1)=(132)=78\binom{11 + 3 - 1}{3 - 1} = \binom{13}{2} = 78.

Example 4 (Linear Inequalities Using a Slack Variable): Find the number of non-negative integer solutions to the inequality:

x1+x2+x3+x4≤12x_1 + x_2 + x_3 + x_4 \le 12

Solution: Introduce a slack variable x5=12−(x1+x2+x3+x4)x_5 = 12 - (x_1 + x_2 + x_3 + x_4) to absorb whatever is "left over" below 1212. Clearly x1+x2+x3+x4≤12  ⟺  x5≥0x_1 + x_2 + x_3 + x_4 \le 12 \iff x_5 \ge 0, turning the inequality into an exact equation in 55 non-negative variables:

x1+x2+x3+x4+x5=12,xi≥0x_1 + x_2 + x_3 + x_4 + x_5 = 12, \qquad x_i \ge 0

Hence the number of solutions is:

(12+5−15−1)=(164)=1820\binom{12 + 5 - 1}{5 - 1} = \binom{16}{4} = 1820

Handling Upper Bounds on Variables§

Now ask the natural question: what if the fruit shop does not have a sufficient supply of mangoes? Suppose the shop has only 1515 mangoes in stock while you want to buy 2020 fruits. Now x1x_1 carries an upper bound: 0≤x1≤150 \le x_1 \le 15.

Why are upper bounds harder than lower bounds? A variable shift x1=y1+a1x_1 = y_1 + a_1 shifts a lower bound to 00, but cannot eliminate an upper bound. Instead, look at the opposite of an upper bound: the opposite of x1≤15x_1 \le 15 is the lower bound x1≥16x_1 \ge 16—which we do know how to count instantly!

This pairs upper bounds cleanly with the Subtraction Principle (for a single upper bound) and with the two-set overlap formula ∣A1∪A2∣=∣A1∣+∣A2∣−∣A1∩A2∣|A_1 \cup A_2| = |A_1| + |A_2| - |A_1 \cap A_2| from Section 01 (for two upper bounds). When three or more upper bounds apply simultaneously, we use the general Principle of Inclusion–Exclusion, developed in full in Section 08.

Example 5 (Single Upper Bound): Find the number of ways to buy 2020 fruits (x1+x2+x3=20x_1 + x_2 + x_3 = 20, xi≥0x_i \ge 0) if the shop has only 1515 mangoes (x1≤15x_1 \le 15), while oranges and avocados are in unlimited supply.

Solution:

  • Unrestricted total (UU): (20+3−12)=(222)=231\binom{20 + 3 - 1}{2} = \binom{22}{2} = 231.
  • Opposite condition (A′A' — violating the upper bound): x1≥16x_1 \ge 16. Shift x1=y1+16x_1 = y_1 + 16 (y1≥0y_1 \ge 0) to get y1+x2+x3=4y_1 + x_2 + x_3 = 4, which has (4+3−12)=(62)=15\binom{4 + 3 - 1}{2} = \binom{6}{2} = 15 invalid solutions.

By the Subtraction Principle, the number of valid purchases is:

231−15=216231 - 15 = 216

Example 6 (Two Upper Bounds): Find the number of non-negative integer solutions to x1+x2+x3=15x_1 + x_2 + x_3 = 15 subject to x1≤6x_1 \le 6 and x2≤7x_2 \le 7 (with no upper bound on x3x_3).

Solution: Let UU be all non-negative integer solutions of x1+x2+x3=15x_1 + x_2 + x_3 = 15, so ∣U∣=(15+3−12)=(172)=136|U| = \binom{15 + 3 - 1}{2} = \binom{17}{2} = 136. Define the two violation sets:

  • A1A_1 (x1≥7x_1 \ge 7): Shift by 7  ⟹  7 \implies sum =8  ⟹  ∣A1∣=(8+22)=(102)=45= 8 \implies |A_1| = \binom{8 + 2}{2} = \binom{10}{2} = 45.
  • A2A_2 (x2≥8x_2 \ge 8): Shift by 8  ⟹  8 \implies sum =7  ⟹  ∣A2∣=(7+22)=(92)=36= 7 \implies |A_2| = \binom{7 + 2}{2} = \binom{9}{2} = 36.
  • A1∩A2A_1 \cap A_2 (x1≥7x_1 \ge 7 and x2≥8x_2 \ge 8): Shift sum by 15  ⟹  15 \implies remaining sum =0  ⟹  ∣A1∩A2∣=(0+22)=1= 0 \implies |A_1 \cap A_2| = \binom{0 + 2}{2} = 1 (namely (7,8,0)(7, 8, 0)).

The number of invalid solutions violating at least one upper bound is ∣A1∪A2∣=∣A1∣+∣A2∣−∣A1∩A2∣=45+36−1=80|A_1 \cup A_2| = |A_1| + |A_2| - |A_1 \cap A_2| = 45 + 36 - 1 = 80. Subtracting from ∣U∣|U| gives:

136−80=56 valid solutions136 - 80 = 56 \text{ valid solutions}

(In Section 08, we will immediately extend this exact example by adding a third upper bound x3≤8x_3 \le 8 using the full Principle of Inclusion–Exclusion!)


Summary Table§

Equation / Constraint Variable Conditions Number of Integer Solutions
x1+x2+⋯+xk=nx_1 + x_2 + \dots + x_k = n xi≥0x_i \ge 0 (n+k−1k−1)=(n+k−1n)\binom{n + k - 1}{k - 1} = \binom{n + k - 1}{n}
x1+x2+⋯+xk=nx_1 + x_2 + \dots + x_k = n xi≥1x_i \ge 1 (n≥kn \ge k) (n−1k−1)\binom{n - 1}{k - 1}
x1+x2+⋯+xk=nx_1 + x_2 + \dots + x_k = n xi≥aix_i \ge a_i, N=n−∑ai≥0N = n - \sum a_i \ge 0 (N+k−1k−1)\binom{N + k - 1}{k - 1}
x1+x2+⋯+xk≤nx_1 + x_2 + \dots + x_k \le n xi≥0x_i \ge 0 (n+kk)\binom{n + k}{k} (introduce slack variable xk+1≥0x_{k+1} \ge 0)
x1+x2+⋯+xk=nx_1 + x_2 + \dots + x_k = n 0≤xi≤bi0 \le x_i \le b_i Count violations xi≥bi+1x_i \ge b_i + 1 and apply Subtraction / PIE
Section 7 of 11 · Combinatorics

Mark this section complete when you have worked through its proofs.