MAT2033 · Week 10 · advanced counting

Advanced counting: selections with repetition, the binomial theorem and recurrence relations

Permutations and combinations with repetition (stars and bars), words with repeated letters, the binomial theorem and Pascal's identity, the next permutation in lexicographic order, and building recurrence relations from problems. A final exam topic.

permutations with repetitioncombinations with repetitionstars and barspermutations of indistinguishable objectsbinomial coefficientbinomial theoremPascal's identitycombinatorial prooflexicographic ordernext permutationrecurrence relationinitial conditionsTower of Hanoi

Last week we counted selections without repetition. This week we allow repetition, see the binomial theorem, and learn to turn a problem into a recurrence relation. It is a final exam topic. Next week (solving linear recurrences) is the continuation. Golden rules:

  1. The four-box table: does order matter, is there repetition? Choose one of the four formulas.
  2. Combination with repetition = stars and bars. rr stars, n−1n - 1 bars.
  3. When finding a binomial coefficient, raise the numbers inside and the sign to the power as well.
  4. When building a recurrence, look at the last step and always write the initial conditions.

The four-formula table

You are choosing rr items from nn types:

No repetitionRepetition
Order mattersP(n,r)=n!(n−r)!P(n,r) = \frac{n!}{(n-r)!}nrn^r
Order does not matterC(n,r)=n!r!(n−r)!C(n,r) = \frac{n!}{r!(n-r)!}C(n+r−1,r)C(n + r - 1, r)

Example (order matters, repetition allowed): a word of length 5 from 26 letters, where letters may repeat: 265=1188137626^5 = 11881376.

Combinations with repetition: stars and bars

Example. A bakery has 4 kinds of pastry, and you will buy 6. You can take as many of each kind as you like, order does not matter. How many different purchases?

Write a purchase with 6 stars and 3 bars that separate the kinds:

**|*||***    ->  type1: 2, type2: 1, type3: 0, type4: 3
|******||    ->  type1: 0, type2: 6, type3: 0, type4: 0

Each purchase corresponds to choosing the positions of the 3 bars in a string of 9 characters (a one-to-one correspondence, a bijection):

C(6+4−1,3)=C(9,3)=C(9,6)=84.C(6 + 4 - 1, 3) = C(9, 3) = C(9, 6) = 84.

Counting solutions of an equation

The nonnegative integer solutions of x1+x2+x3=11x_1 + x_2 + x_3 = 11: 11 stars, 2 bars, C(13,2)=78C(13, 2) = 78.

If each xi≥1x_i \ge 1 is required: first put 1 in each box, leaving 8. y1+y2+y3=8y_1 + y_2 + y_3 = 8, C(10,2)=45C(10, 2) = 45.

The same model: distributing 8 identical balls into 4 different boxes is C(8+3,3)=C(11,3)=165C(8 + 3, 3) = C(11, 3) = 165 ways.

One more example: distribute 10 identical candies to 3 children so that each child gets at least 2. First give each child 2 (3⋅2=63 \cdot 2 = 6 candies gone), and the remaining 4 candies are free: y1+y2+y3=4y_1 + y_2 + y_3 = 4, C(4+2,2)=C(6,2)=15C(4 + 2, 2) = C(6, 2) = 15 ways. Every question with lower bounds finishes in these two steps: first hand out the mandatory share, then count the rest with stars and bars.

PITFALL · identical or distinct objects? Stars and bars works only for identical objects (the same balls, the same kind of pastry). If you throw 8 distinct balls into 4 boxes, each ball chooses one of the 4 boxes: 484^8.

Words with repeated letters

If among nn objects n1n_1 are of one kind, n2n_2 of another kind, …, the number of distinct arrangements is

n!n1! n2!⋯nk!.\frac{n!}{n_1!\, n_2! \cdots n_k!}.

BANANA: 6 letters, A three times, N twice, B once. 6!3! 2! 1!=72012=60\frac{6!}{3!\,2!\,1!} = \frac{720}{12} = 60. Why do we divide? Swapping the three A’s among themselves (3!3!) gives the same word, so we remove those repeats (the division rule).

The binomial theorem

(x+y)n=∑j=0n(nj)xn−jyj.(x + y)^n = \sum_{j=0}^{n} \binom{n}{j} x^{n-j} y^j.

Intuition: in the product (x+y)(x+y)⋯(x+y)(x+y)(x+y)\cdots(x+y) you pick either xx or yy from each parenthesis. How many times the term xn−jyjx^{n-j}y^j appears = the number of ways to choose the jj parentheses from which you pick yy = (nj)\binom{n}{j}.

Example: (x+y)4=x4+4x3y+6x2y2+4xy3+y4(x + y)^4 = x^4 + 4x^3y + 6x^2y^2 + 4xy^3 + y^4. The coefficients are 1, 4, 6, 4, 1.

Worked example. The coefficient of x3y4x^3y^4 in the expansion of (2x−y)7(2x - y)^7.

General term: C(7, j) (2x)^(7-j) (-y)^j
For x^3 y^4, j = 4
C(7, 4) * 2^3 * (-1)^4 = 35 * 8 * 1 = 280

Two important results:

∑k=0n(nk)=2n(x=y=1),∑k=0n(−1)k(nk)=0(x=1, y=−1, n≥1).\sum_{k=0}^{n}\binom{n}{k} = 2^n \quad (x = y = 1), \qquad \sum_{k=0}^{n}(-1)^k\binom{n}{k} = 0 \quad (x = 1,\ y = -1,\ n \ge 1).

Example: (50)+⋯+(55)=1+5+10+10+5+1=32=25\binom{5}{0} + \dots + \binom{5}{5} = 1 + 5 + 10 + 10 + 5 + 1 = 32 = 2^5.

Pascal’s identity and combinatorial proofs

(n+1k)=(nk−1)+(nk).\binom{n+1}{k} = \binom{n}{k-1} + \binom{n}{k}.

Combinatorial proof: show that both sides count the same thing.

Proof. Let S be a set with n + 1 elements and fix one element a in S.
The left side counts the k-element subsets of S.
Such a subset either contains a or does not.
- If it contains a, choose the other k - 1 elements from the
  remaining n elements: C(n, k-1) ways.
- If it does not contain a, choose all k elements from the
  remaining n elements: C(n, k) ways.
By the sum rule the total is C(n, k-1) + C(n, k). ∎

Check: (62)=15\binom{6}{2} = 15, (51)+(52)=5+10=15\binom{5}{1} + \binom{5}{2} = 5 + 10 = 15. In Pascal’s triangle each number is the sum of the two numbers above it.

Generating permutations: lexicographic order

The permutation that comes after a1a2…ana_1 a_2 \dots a_n in lexicographic order:

  1. Going from right to left, find the first jj with aj<aj+1a_j < a_{j+1}.
  2. Among the elements to the right of aja_j, find the smallest one greater than aja_j and swap it with aja_j.
  3. Arrange the part to the right of position jj in increasing order.
362541
From the right: 4 > 1, 5 > 4, 2 < 5  ->  a_j = 2
To the right of 2: 5 4 1, the smallest greater than 2 = 4
Swap: 364521
Sort the right of 4 increasing: 1 2 5  ->  364125

Recurrence relations

A recurrence relation expresses the term ana_n of a sequence in terms of earlier terms. Without initial conditions the sequence is not determined.

Example 1 (interest). 1000 TL at 5% annual interest: Pn=1.05 Pn−1P_n = 1.05\,P_{n-1}, P0=1000P_0 = 1000. After three years, P3=1000⋅1.053=1157.625P_3 = 1000 \cdot 1.05^3 = 1157.625.

Example 2 (Tower of Hanoi). To move nn disks: move the top n−1n-1 disks to the spare peg (Hn−1H_{n-1}), move the largest (1), move the n−1n-1 disks on top of it (Hn−1H_{n-1}):

Hn=2Hn−1+1,H1=1.H_n = 2H_{n-1} + 1, \qquad H_1 = 1.

By iteration 1,3,7,15,31,631, 3, 7, 15, 31, 63: the guess is Hn=2n−1H_n = 2^n - 1. Its proof by induction (the template of week 3):

Claim. H_n = 2^n - 1 for all integers n >= 1.
Basis step. n = 1: H_1 = 1 and 2^1 - 1 = 1. True.
Inductive step. Assume H_k = 2^k - 1 for some k >= 1 (IH).
Then H_(k+1) = 2 H_k + 1 = 2(2^k - 1) + 1 = 2^(k+1) - 2 + 1 = 2^(k+1) - 1.
So the claim holds for k + 1.
By mathematical induction, H_n = 2^n - 1 for all n >= 1. ∎

Example 3 (bit strings). ana_n: the number of bit strings of length nn that do not contain two consecutive 0s. Look at the last bit:

  • Last bit 1: the part before it is any valid string of length n−1n-1, an−1a_{n-1}.
  • Last bit 0: the bit before it must be 1, and the part before that is a valid string of length n−2n-2, an−2a_{n-2}.

an=an−1+an−2,a1=2, a2=3.a_n = a_{n-1} + a_{n-2}, \qquad a_1 = 2,\ a_2 = 3.

The values are 2,3,5,8,13,212, 3, 5, 8, 13, 21: the Fibonacci sequence, shifted.

TIP · the “look at the last step” recipe. When building a recurrence, split into cases by the last element of the sequence (the last bit, the last disk moved, the last digit). In each case the remaining part is the same problem, only shorter. Add up the cases.

Common mistakes

Wrong ideaThe truth
Combination with repetition is C(n,r)C(n, r)C(n+r−1,r)C(n + r - 1, r)
nn bars in stars and barsFor nn boxes, n−1n - 1 bars
Stars and bars for distinct objects tooDistinct objects into nn boxes: nrn^r
Ignoring the condition xi≥1x_i \ge 1First give 1 to each box, count for the remaining r−nr - n
BANANA =6!= 6!Repeats are divided out: 6!/(3! 2!)=606!/(3!\,2!) = 60
The coefficient of x4x^4 in (3x−2)6(3x-2)^6 is (64)\binom{6}{4}The numbers are raised to the power too: (64)34(−2)2=4860\binom{6}{4} 3^4 (-2)^2 = 4860
Writing the recurrence and forgetting the initial conditionWithout a1a_1, a2a_2 the sequence is undefined
Leaving the right part in reverse order in the next permutationAfter the swap, the right part is arranged in increasing order

Test yourself

Exam-style questions. Solve them on paper first, then reveal the answer.

Question 1easy

How many strings of length 4 can be formed from the letters A, B, C if letters may be repeated?

Show answer
81

Permutation with repetition: 3 choices for each position, 34=813^4 = 81.

Question 2medium

An ice cream shop has 3 flavors. In how many ways can you choose 5 scoops (order does not matter, flavors may repeat)?

Show answer
21

Combination with repetition: n=3n = 3 types, r=5r = 5 selections. C(n+r−1,r)=C(7,5)=C(7,2)=21C(n + r - 1, r) = C(7, 5) = C(7, 2) = 21. Stars and bars: 5 stars, 2 bars, choose the places of the 2 bars among 7 places.

Question 3medium

How many nonnegative integer solutions does x1+x2+x3+x4=10x_1 + x_2 + x_3 + x_4 = 10 have?

Show answer
286

You are distributing 10 stars into 4 boxes, so you need 3 bars. C(10+4−1,4−1)=C(13,3)=13⋅12⋅116=286C(10 + 4 - 1, 4 - 1) = C(13, 3) = \frac{13 \cdot 12 \cdot 11}{6} = 286.

Question 4hard

How many integer solutions does x1+x2+x3=15x_1 + x_2 + x_3 = 15 have with x1≥2x_1 \ge 2, x2≥3x_2 \ge 3, x3≥0x_3 \ge 0?

Show answer
66

Give out the lower bounds first: x1=y1+2x_1 = y_1 + 2, x2=y2+3x_2 = y_2 + 3, x3=y3x_3 = y_3. The new equation is y1+y2+y3=10y_1 + y_2 + y_3 = 10, yi≥0y_i \ge 0. The number of solutions is C(10+2,2)=C(12,2)=66C(10 + 2, 2) = C(12, 2) = 66.

Question 5medium

How many different strings can be made by rearranging the letters of LETTER?

Show answer
180

6 letters: L, E, T, T, E, R. E twice, T twice. 6!2! 2! 1! 1!=7204=180\frac{6!}{2!\,2!\,1!\,1!} = \frac{720}{4} = 180.

Question 6easy

What is the coefficient of x2y5x^2 y^5 in the expansion of (x+y)7(x + y)^7?

Show answer
21

Binomial theorem: the coefficient of the term xn−jyjx^{n-j} y^j is (nj)\binom{n}{j}. n=7n = 7, j=5j = 5: (75)=(72)=21\binom{7}{5} = \binom{7}{2} = 21.

Question 7medium

What is the coefficient of x4x^4 in the expansion of (3x−2)6(3x - 2)^6?

Show answer
4860

The general term is (6k)(3x)k(−2)6−k\binom{6}{k}(3x)^k(-2)^{6-k}. For x4x^4, k=4k = 4: (64)⋅34⋅(−2)2=15⋅81⋅4=4860\binom{6}{4} \cdot 3^4 \cdot (-2)^2 = 15 \cdot 81 \cdot 4 = 4860. Sign: (−2)2(-2)^2 is positive.

Question 8easy

Evaluate ∑k=08(8k)\displaystyle\sum_{k=0}^{8} \binom{8}{k}.

Show answer
256

In the binomial theorem take x=y=1x = y = 1: (1+1)8=∑k(8k)(1 + 1)^8 = \sum_k \binom{8}{k}. So 28=2562^8 = 256. Combinatorial meaning: all the subsets of an 8-element set.

Question 9medium

Evaluate ∑k=09(−1)k(9k)\displaystyle\sum_{k=0}^{9} (-1)^k \binom{9}{k}.

Show answer
0

In the binomial theorem take x=1x = 1, y=−1y = -1: (1−1)9=∑k(9k)19−k(−1)k=09=0(1 - 1)^9 = \sum_k \binom{9}{k} 1^{9-k}(-1)^k = 0^9 = 0.

Question 10medium

The Tower of Hanoi numbers satisfy Hn=2Hn−1+1H_n = 2H_{n-1} + 1 with H1=1H_1 = 1. Find H8H_8.

Show answer
255

Iteration: H1=1H_1 = 1, H2=3H_2 = 3, H3=7H_3 = 7, H4=15H_4 = 15, H5=31H_5 = 31, H6=63H_6 = 63, H7=127H_7 = 127, H8=255H_8 = 255. With the closed form Hn=2n−1H_n = 2^n - 1 too, 28−1=2552^8 - 1 = 255.

Question 11hard

Let ana_n be the number of strings of length nn over {0,1,2}\{0, 1, 2\} that contain no two consecutive 0s. Find a recurrence for ana_n with initial conditions, and compute a5a_5.

Show answer
a_n = 2a_{n-1} + 2a_{n-2}, a_1 = 3, a_2 = 8; a_5 = 164

Look at the last character. If the last character is 1 or 2 (2 choices), the part before it is any valid string of length n−1n-1: 2an−12a_{n-1}. If the last character is 0, the one before it cannot be 0, so it must be 1 or 2 (2 choices), and the part before that is a valid string of length n−2n-2: 2an−22a_{n-2}. Initial: a1=3a_1 = 3, a2=9−1=8a_2 = 9 - 1 = 8 (only 00 is forbidden). Then a3=2⋅8+2⋅3=22a_3 = 2 \cdot 8 + 2 \cdot 3 = 22, a4=2⋅22+2⋅8=60a_4 = 2 \cdot 22 + 2 \cdot 8 = 60, a5=2⋅60+2⋅22=164a_5 = 2 \cdot 60 + 2 \cdot 22 = 164.

Question 12medium

Find the permutation that comes right after 25431 in lexicographic order.

Show answer
31245

Going from right to left, find the first "small-large" pair: 2 < 5 (position 1). In the 5431 to the right of 2, the smallest number greater than 2 is 3. Swap 2 and 3: 35421. Arrange the part to the right of 3 in increasing order: 1245. Result 31245.

Verification: All counts were verified with node in two ways, by formula and by exact counting (generating the solutions, words and strings one by one); binomial coefficients by polynomial multiplication, next permutations by the algorithm and by sorting all permutations, and recurrence values by iteration and brute-force counting (verify/MAT2033/10.mjs).

Review card

Permutation with repetition (order matters): n^r Combination with repetition (order does not matter): C(n + r - 1, r) = C(n + r - 1, n - 1) x1 + ... + xn = r, xi >= 0 -> C(r + n - 1, n - 1) (r stars, n-1 bars) Lower bound xi >= a -> give out a first, subtract from r Repeated letters: n! / (n1! n2! ... nk!) BANANA = 6!/(3!2!) = 60 Binomial: (x+y)^n = sum C(n,j) x^(n-j) y^j When finding a coefficient, raise the numbers AND the SIGN to the power: (3x-2)^6 -> C(6,4) 3^4 (-2)^2 Sum C(n,k) = 2^n; alternating sum = 0 Pascal: C(n+1,k) = C(n,k-1) + C(n,k) Next permutation: from the right the first a_j < a_(j+1); swap with the smallest larger one on the right; sort the right part increasing Recurrence: look at the last step, split into smaller cases; WRITE THE INITIAL CONDITIONS Hanoi H_n = 2H_(n-1) + 1 = 2^n - 1; bit strings with "no 00" a_n = a_(n-1) + a_(n-2)