Sub topic 13
Distributing Objects and Counting Integer Solutions
This chapter assumes you know factorials, nCr, and the idea that a combination counts selections where order does not matter. You should also be comfortable with the symmetry property nCr = nC(n−r). The chapter turns many "how many ways to share identical items" and "how many solutions does this equation have" problems into one nCr computation.
The Core Idea
Suppose you must give 10 identical sweets to 4 children. The sweets are identical, so only the number each child receives matters. Writing the counts as (x1, x2, x3, x4), you need whole numbers that add up to 10.
This is the same problem as counting the non-negative integer solutions of
x1 + x2 + x3 + x4 = 10
Each solution is one distribution, and each distribution is one solution. Listing them is hopeless, so we use a counting trick called stars and bars.
Stars and Bars
Draw the 10 identical objects as stars. To split them among 4 children, place 3 bars (dividers) among the stars. The bars cut the row into 4 blocks, and the size of each block is one child's share.
For example, the row **|***||***** has 3 bars. It gives the shares 2, 3, 0, 5. An empty block means that child gets nothing, which is allowed here.
Every arrangement of 10 stars and 3 bars gives exactly one distribution, and every distribution gives exactly one arrangement. So we count arrangements: 13 positions in total, and we choose which 3 hold bars.
Formula for non-negative solutions
The number of non-negative integer solutions of x1 + x2 + ... + xk = n is
C(n + k − 1, k − 1)
Here n is the number of stars, k is the number of variables (or recipients), and k − 1 is the number of bars. The total number of positions is n + k − 1.
For x1 + x2 + x3 + x4 = 10, we get C(13, 3) = 286.
Formula for positive solutions
If every variable must be at least 1, give one object to each variable first. That uses k objects and leaves n − k to distribute freely. The count is
C(n − k + k − 1, k − 1) = C(n − 1, k − 1)
For the same equation with x1, x2, x3, x4 ≥ 1, we get C(9, 3) = 84.
Both results are checked in the code below.
import math
# Non-negative integer solutions of x1 + x2 + x3 + x4 = 10
print(math.comb(10 + 4 - 1, 4 - 1)) # 286
# Positive integer solutions of the same equation
print(math.comb(10 - 1, 4 - 1)) # 84Minimum Value Conditions
Many CAT problems set a lower limit other than 0 or 1. The method is always the same: pre-assign the minimum to each variable, then distribute what remains with no restriction.
Example: find the number of solutions of x + y + z = 20 where x ≥ 2, y ≥ 3, z ≥ 5.
- Pre-assign 2, 3 and 5. This uses 10 objects.
- Distribute the remaining 10 among 3 variables with no restriction.
- Count = C(10 + 3 − 1, 3 − 1) = C(12, 2) = 66.
A cleaner way to say this is a substitution. Let x' = x − 2, y' = y − 3, z' = z − 5. Then x' + y' + z' = 10 with all variables ≥ 0.
Upper Limits: Use Complement
Stars and bars has no built-in way to cap a variable. When a variable has an upper limit, count all solutions and subtract the ones that break the limit.
Example: count non-negative solutions of x + y + z = 10 with x ≤ 4.
- All solutions with no limit: C(12, 2) = 66.
- Bad solutions have x ≥ 5. Substitute x' = x − 5, so x' + y + z = 5. The count is C(7, 2) = 21.
- Answer: 66 − 21 = 45.
If two variables can both break their limits, you need inclusion-exclusion, which a later chapter covers. For small totals, check whether two violations can happen together. If the limits are 5 and the total is 8, two violations need at least 10 objects, so they cannot both occur.
Objects That Are Distinct
Stars and bars applies only when the objects are identical. When the objects are different, each object independently picks a recipient.
Example: in how many ways can 5 different books be given to 3 students, with no other conditions? Each book has 3 choices, so the answer is 3^5 = 243.
The contrast matters. Compare these two questions, each with 3 recipients:
| Objects | Condition | Method | Count |
|---|---|---|---|
| 5 identical balls | Any recipient may get none | C(5 + 2, 2) | 21 |
| 5 identical balls | Every recipient gets at least 1 | C(4, 2) | 6 |
| 5 distinct books | Any recipient may get none | 3^5 | 243 |
If you are unsure which case applies, ask: "If I swap two objects, do I get a different outcome?" If yes, the objects are distinct. If no, they are identical.
Selecting with Repetition
Stars and bars also counts selections where you may pick the same type more than once. Suppose a shop sells 5 flavours of ice cream, and you buy 8 scoops in any combination. Only the number of scoops of each flavour matters. This is the equation x1 + ... + x5 = 8 in non-negative integers, so the count is C(8 + 4, 4) = C(12, 4) = 495.
If you must have at least one scoop of every flavour, use C(7, 4) = 35.
Counting Solutions with a Sum Condition on Digits
A classic CAT variant asks how many numbers have digits with a given sum. For example: how many 3-digit numbers have digit sum 12?
Let the digits be a, b, c with a ≥ 1, b ≥ 0, c ≥ 0, and each digit at most 9.
- Put a' = a − 1, so a' + b + c = 11 with all variables ≥ 0. The count is C(13, 2) = 78.
- Remove cases where a digit exceeds 9. If a' ≥ 9 (that is, a ≥ 10), put a'' = a' − 9, giving a'' + b + c = 2, with C(4, 2) = 6 solutions. If b ≥ 10, put b' = b − 10, giving a' + b' + c = 1, with C(3, 2) = 3 solutions. The same holds for c: 3 solutions.
- Two digits cannot both be too large, since that needs more than 11 in total.
- Answer: 78 − 6 − 3 − 3 = 66.
You can confirm this by brute force.
count = sum(1 for n in range(100, 1000) if sum(map(int, str(n))) == 12)
print(count) # 66Common Mistakes
- Using stars and bars for distinct objects. The formula needs identical objects.
- Wrong bar count. With k recipients you need k − 1 bars, not k.
- Forgetting that 0 is allowed. "Non-negative" gives C(n + k − 1, k − 1). "Positive" gives C(n − 1, k − 1). Read the wording for "at least one", "each", or "none may be empty".
- Not subtracting minimums before applying the formula. Using n itself when a variable has a lower bound overcounts.
- Ignoring upper limits. Natural digit and box-capacity limits (digits at most 9) are easy to miss.
- Mixing up variables and objects. In C(n + k − 1, k − 1), n is the total to be shared and k is the number of variables. Swapping them gives a wrong, but similar-looking, answer.
A Quick Decision Guide
Exercises
Exercise
How many non-negative integer solutions does x + y + z + w = 15 have if x ≥ 1 and y ≥ 2?
Show answer
Subtract the minimums: x' = x − 1 and y' = y − 2, so x' + y' + z + w = 12 with all variables ≥ 0. The count is C(12 + 3, 3) = C(15, 3) = 455.
Exercise
In how many ways can 12 identical pencils be given to 4 students so that every student gets at least 2 pencils?
Show answer
Give 2 pencils to each student first, using 8. Distribute the remaining 4 freely among 4 students: C(4 + 3, 3) = C(7, 3) = 35.
Exercise
Count the non-negative solutions of a + b + c = 12 where a ≤ 5.
Show answer
Without the limit there are C(14, 2) = 91 solutions. Solutions with a ≥ 6: put a' = a − 6, so a' + b + c = 6, which has C(8, 2) = 28 solutions. The answer is 91 − 28 = 63.