MAT2033 · Week 9 · introduction to counting, the pigeonhole principle

Introduction to counting and the pigeonhole principle

Product and sum rules, inclusion-exclusion, the pigeonhole principle (including the generalized form), permutations P(n,r) and combinations C(n,r). A final exam topic, not on this semester's midterm.

product rulesum rulesubtraction rule (inclusion-exclusion)division ruletree diagrampigeonhole principlegeneralized pigeonhole principlepermutationr-permutationcombinationr-combinationfactorial

Counting starts this week. It is not on this semester’s midterm, it is a final exam topic. But combinatorics is one of the places where the most marks turn on an exam, because the questions are short and the answer is a single number. The point is choosing the right rule to apply. Golden rules:

  1. “And” multiplies, “or” adds (if the options do not overlap).
  2. If there is overlap, use inclusion-exclusion: subtract once what you counted twice.
  3. An “at least one” question is solved with the complement: everything minus the ones with none.
  4. If order matters it is a permutation, if not a combination. Ask yourself in every question: “are AB and BA different?”
  5. In the pigeonhole principle, name the box and the object explicitly. Half the marks are in that sentence.

Basic counting rules

Product rule

If a task is done in two consecutive steps, and the first step can be done in n1n_1 ways and the second step (in every case) in n2n_2 ways, the task can be done in n1⋅n2n_1 \cdot n_2 ways.

Example: a menu with one of 3 starters, 4 main courses and 2 desserts: 3⋅4⋅2=243 \cdot 4 \cdot 2 = 24 menus. If you draw a tree diagram you get 24 leaves.

The most important consequences of the product rule:

  • Bit strings of length nn: 2n2^n.
  • Functions from an mm-element set to an nn-element set: nn choices for each element, nmn^m.
  • Subsets of an nn-element set: each element is either in or out, 2n2^n (the power set of week 4).

Sum rule

If a task is done either in the first way (n1n_1 options) or in the second way (n2n_2 options), and there are no common options: n1+n2n_1 + n_2. Example: project list A has 12 topics, list B has 9, and there are no common topics. There are 12+9=2112 + 9 = 21 ways to choose a topic.

Subtraction rule (inclusion-exclusion)

If there are common options you count them twice, so you subtract them once:

∣A∪B∣=∣A∣+∣B∣−∣A∩B∣.\lvert A \cup B \rvert = \lvert A \rvert + \lvert B \rvert - \lvert A \cap B \rvert.

Example. How many 7-bit strings begin with 1 or end with 00?

begin with 1:             the remaining 6 bits are free  -> 2^6 = 64
end with 00:              the remaining 5 bits are free  -> 2^5 = 32
both (1....00):           the 4 middle bits              -> 2^4 = 16
Total: 64 + 32 - 16 = 80

“At least one” and the complement

Example. A 4-character password, where each character is an uppercase letter (26) or a digit (10). How many passwords contain at least one digit?

Counting directly is messy (one digit, two digits, …). With the complement:

364⏟all−264⏟no digit=1679616−456976=1222640.\underbrace{36^4}_{\text{all}} - \underbrace{26^4}_{\text{no digit}} = 1679616 - 456976 = 1222640.

PITFALL · Doing “at least one” with the product rule. Computing 4⋅10⋅3634 \cdot 10 \cdot 36^3 by “make one position a digit (4 ways), 10 digits for that position, the other 3 positions 36” is wrong: it counts passwords with two digits more than once. The complement method is always safe.

Division rule

If you count every outcome exactly dd times, you divide the total by dd. Example: 5 people at a round table. In a straight line it is 5!=1205! = 120, but rotations count as the same seating, and each seating was counted 5 times: 120/5=24120/5 = 24.

The pigeonhole principle

Principle. If you put k+1k + 1 (or more) objects into kk boxes, at least one box has at least two objects.

Proof. Suppose, for contradiction, that k + 1 objects are placed
into k boxes and no box contains more than one object.
Then the total number of objects is at most k * 1 = k.
This contradicts having k + 1 objects. Hence some box contains
at least two objects. ∎

Generalized principle. If you put NN objects into kk boxes, some box has at least ⌈N/k⌉\lceil N/k \rceil objects.

Example: in a class of 40 students, at least ⌈40/12⌉=4\lceil 40/12 \rceil = 4 were born in the same month. Reason: if each month had at most 3 people, the total would be at most 12⋅3=36<4012 \cdot 3 = 36 < 40.

“At least how many to guarantee?” questions

Recipe: set up the worst case, add one. Grades are 5 letters (A, B, C, D, F). How many students must a class have to guarantee that 5 students got the same grade? Worst case: 4 students of each grade, 5⋅4=205 \cdot 4 = 20 students and still no 5 the same. One more person: 2121.

General pattern: kk boxes, if you want rr of the same, the answer is k(r−1)+1k(r-1) + 1.

TIP · choose the box correctly. If the question says “two numbers whose difference is divisible by 4”, the boxes are the remainders mod 4. If it says “two numbers whose sum is 10”, the boxes are the pairs {1,9},{2,8},…\{1, 9\}, \{2, 8\}, \dots. The moment you find the box, the question is over.

Permutations and combinations

Permutation (r-permutation): arranging rr of nn distinct objects in order.

P(n,r)=n(n−1)⋯(n−r+1)=n!(n−r)!.P(n, r) = n(n-1)\cdots(n-r+1) = \frac{n!}{(n-r)!}.

Combination (r-combination): choosing rr of nn distinct objects without order (a subset).

C(n,r)=(nr)=n!r! (n−r)!.C(n, r) = \binom{n}{r} = \frac{n!}{r!\,(n-r)!}.

Connection: P(n,r)=C(n,r)⋅r!P(n, r) = C(n, r) \cdot r!. First which rr objects (combination), then in how many ways you arrange them (r!r!).

QuestionDoes order matter?Answer
A president and a vice president from 9 peopleYesP(9,2)=9⋅8=72P(9,2) = 9 \cdot 8 = 72
A team of 4 from 10 peopleNoC(10,4)=210C(10,4) = 210
A team of 6 from 10 peopleNoC(10,6)=C(10,4)=210C(10,6) = C(10,4) = 210
The positions of the (three) 1s in a 10-bit stringNoC(10,3)=120C(10,3) = 120

C(n,r)=C(n,n−r)C(n, r) = C(n, n - r): choosing 4 people is the same job as choosing the 6 who stay out.

TIP · simplify factorials by hand. There is no calculator. For C(10,4)C(10,4) don’t compute 10!10!: write 10⋅9⋅8⋅74⋅3⋅2⋅1\frac{10 \cdot 9 \cdot 8 \cdot 7}{4 \cdot 3 \cdot 2 \cdot 1} and simplify first. 8/(4⋅2)=18 / (4 \cdot 2) = 1, 9/3=39 / 3 = 3, leaving 10⋅3⋅7=21010 \cdot 3 \cdot 7 = 210. The denominator always divides evenly, because the result is a count.

Worked example: restricted ordering

6 people line up, and A and B must be next to each other.

1) Make A and B a single block: [AB] + 4 people = 5 objects
2) Arrange the 5 objects: 5! = 120
3) Inside the block AB or BA: 2
4) Product rule: 2 * 120 = 240

If it asks for A and B not next to each other, use the complement: 6!−240=720−240=4806! - 240 = 720 - 240 = 480.

Which rule? Decision order

When you read the question, ask yourself in this order:

  1. How many steps is it built in? If there are consecutive steps, the product rule.
  2. Are there separate cases? If it splits into “either this or that”, count each case separately and add.
  3. Do the cases overlap? If they overlap, subtract the intersection.
  4. Is there the word “at least” or “at most”? The complement is usually shorter.
  5. Does the order of the chosen items matter? If it matters, PP; if not, CC.

Worked example. How many of the 5-element subsets of {1,2,…,10}\{1, 2, \dots, 10\} contain 1 or 2?

All 5-element subsets:                  C(10,5) = 252
Containing neither 1 nor 2 (5 of 8):    C(8,5)  = 56
Containing at least one (complement):   252 - 56 = 196

Find the same answer with inclusion-exclusion too: containing 1 is C(9,4)=126C(9,4) = 126, containing 2 is 126126, containing both is C(8,3)=56C(8,3) = 56. 126+126−56=196126 + 126 - 56 = 196. If you get the same number two ways, write it with confidence. If you have time on the exam, do this cross-check.

Common mistakes

Wrong ideaThe truth
Just adding in an overlapping “or”Inclusion-exclusion: subtract the intersection
For divisible by 4 or 6, subtract the multiples of 24Subtract the multiples of lcm⁡(4,6)=12\operatorname{lcm}(4,6) = 12
For “at least one digit”, 4⋅10⋅3634 \cdot 10 \cdot 36^3Complement: 364−26436^4 - 26^4
Medal awarding as C(8,3)C(8,3)Order matters: P(8,3)=336P(8,3) = 336
Team selection as P(10,4)P(10,4)Order does not matter: C(10,4)=210C(10,4) = 210
N/kN/k in the pigeonhole principleRound up: ⌈N/k⌉\lceil N/k \rceil
k⋅rk \cdot r in a “to guarantee” questionWorst case is k(r−1)k(r-1), the answer is k(r−1)+1k(r-1) + 1
Forgetting the ⋅2\cdot 2 after the block methodThe order inside the block (AB, BA) is multiplied separately

Test yourself

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

Question 1easy

A license plate consists of 2 letters (26 choices each) followed by 3 digits. How many plates are possible?

Show answer
676000

Product rule: five positions, each independent. 26⋅26⋅10⋅10⋅10=676⋅1000=67600026 \cdot 26 \cdot 10 \cdot 10 \cdot 10 = 676 \cdot 1000 = 676000.

Question 2easy

How many bit strings of length 10 are there? How many of them contain exactly three 1s?

Show answer
1024; 120

2 choices for each bit: 210=10242^{10} = 1024. Exactly three 1s: choose which 3 of the 10 positions are 1, order does not matter: C(10,3)=10⋅9⋅83!=120C(10,3) = \frac{10 \cdot 9 \cdot 8}{3!} = 120.

Question 3medium

How many bit strings of length 8 begin with 11 or end with 0?

Show answer
160

AA: begins with 11, the remaining 6 bits are free: 26=642^6 = 64. BB: ends with 0: 27=1282^7 = 128. A∩BA \cap B: begins with 11 and ends with 0, the middle 5 bits are free: 25=322^5 = 32. Inclusion-exclusion: 64+128−32=16064 + 128 - 32 = 160.

Question 4medium

How many integers from 1 to 500 (inclusive) are divisible by 4 or by 6?

Show answer
167

Divisible by 4: ⌊500/4⌋=125\lfloor 500/4 \rfloor = 125. Divisible by 6: ⌊500/6⌋=83\lfloor 500/6 \rfloor = 83. Divisible by both = divisible by lcm⁡(4,6)=12\operatorname{lcm}(4,6) = 12 (not 24!): ⌊500/12⌋=41\lfloor 500/12 \rfloor = 41. Total 125+83−41=167125 + 83 - 41 = 167.

Question 5easy

How many people must be in a room to guarantee that at least two of them have first names starting with the same letter (26 letters)?

Show answer
27

The boxes are letters (26), the objects are people. With 26 people, everyone could start with a different letter. The 27th person must land in a box that is already occupied: 26+1=2726 + 1 = 27.

Question 6medium

A drawer contains socks of 5 different colors. How many socks must you take (without looking) to be sure of getting 3 socks of the same color?

Show answer
11

Worst case: 2 socks of each color, 5⋅2=105 \cdot 2 = 10 socks in total and still no 3 of the same color. The next sock, whatever its color, brings that color to 3. Generalized pigeonhole: the smallest NN with ⌈N/5⌉≥3\lceil N/5 \rceil \ge 3 is N=11N = 11.

Question 7medium

A committee of 4 is to be chosen from 7 women and 5 men. How many committees contain exactly 2 women?

Show answer
210

Two independent choices, product rule: 2 of the 7 women is C(7,2)=21C(7,2) = 21, 2 of the 5 men is C(5,2)=10C(5,2) = 10. 21⋅10=21021 \cdot 10 = 210.

Question 8easy

In how many ways can gold, silver and bronze medals be awarded among 8 runners?

Show answer
336

Order matters (gold ≠ silver), no repetition: a permutation. P(8,3)=8⋅7⋅6=336P(8,3) = 8 \cdot 7 \cdot 6 = 336.

Question 9hard

In how many ways can 6 people stand in a row if two particular people, A and B, must stand next to each other?

Show answer
240

Treat A and B as a single block. Block + 4 people = 5 objects, 5!=1205! = 120 orderings. Inside the block AB or BA: 2 choices. Total 2⋅120=2402 \cdot 120 = 240.

Question 10hard

Among 100 people, at least how many must have been born on the same day of the week?

Show answer
15

Generalized pigeonhole: ⌈100/7⌉=⌈14.28⌉=15\lceil 100/7 \rceil = \lceil 14.28 \rceil = 15. Reason: if at most 14 people fell on each day, the total would be at most 7⋅14=98<1007 \cdot 14 = 98 < 100.

Question 11hard

Prove that among any 5 integers, there are two whose difference is divisible by 4.

Show answer
Pigeonhole on the 4 remainders mod 4

Boxes: the remainders mod 4, namely 0, 1, 2, 3 (4 boxes). Objects: the 5 integers. Since 5>45 > 4, by the pigeonhole principle two numbers, say aa and bb, have the same remainder: a mod 4=b mod 4a \bmod 4 = b \bmod 4. Then a≡b(mod4)a \equiv b \pmod 4, that is 4∣(a−b)4 \mid (a - b). ∎ (With 4 numbers the claim can fail: 0, 1, 2, 3.)

Question 12medium

Let ∣A∣=3\lvert A \rvert = 3 and ∣B∣=5\lvert B \rvert = 5. How many functions f:A→Bf: A \to B are there? How many of them are one-to-one?

Show answer
125; 60

Each element of AA goes to one of 5 values: 53=1255^3 = 125. For one-to-one, the values must be distinct: 5⋅4⋅3=P(5,3)=605 \cdot 4 \cdot 3 = P(5,3) = 60.

Verification: All counts were verified with node both by formula and by generating and counting the possibilities one by one (bit strings, passwords, committees, orderings, functions); the pigeonhole claim was scanned over all 5-element choices from 0-11 (verify/MAT2033/09.mjs).

Review card

Product rule: choices step by step -> multiply (and) Sum rule: disjoint options -> add (or, no overlap) Inclusion-exclusion: |A u B| = |A| + |B| - |A n B| "At least one" -> complement: total - none (36^4 - 26^4) "divisible by a or b": floor(N/a) + floor(N/b) - floor(N/lcm(a,b)) Pigeonhole: k+1 objects, k boxes -> some box has at least 2 General: N objects, k boxes -> some box has at least ceil(N/k) "How many to guarantee?": worst case + 1 (k boxes, r of the same: k(r-1)+1) P(n,r) = n!/(n-r)! ORDER MATTERS, no repetition C(n,r) = n!/(r!(n-r)!) ORDER DOES NOT MATTER; C(n,r) = C(n,n-r) Must be adjacent -> make a block, count inside the block separately Function A -> B: |B|^|A|; one-to-one: P(|B|, |A|)