4.1 Counting Principles
Rule of Sum. If task A can be done in m ways and task B in n ways, and they cannot both be Done, then A or B can be done in m+n ways.
Rule of Product. If task A can be done in m ways and task B in n ways independently, then A and B together can be done in mn ways.
4.2 Permutations and Combinations
Permutations: P(n,r)=n!/(n−r)! — ordered arrangements of r items from n.
Combinations: (rn)=r!(n−r)!n! — unordered selections of r items from n.
Theorem 4.1 (Binomial Theorem).
(x+y)n=∑r=0n(rn)xn−ryr
Theorem 4.2 (Pascal”s Identity). (rn)=(rn−1)+(r−1n−1)
Proof. Every r-subset of 1,…,n either contains n (giving (r−1n−1) ways To choose the remaining r−1) or does not contain n (giving (rn−1) ways to choose all r From 1,…,n−1). ■
4.3 Inclusion-Exclusion Principle
Theorem 4.3 (Inclusion-Exclusion). For finite sets A1,…,An:
∣⋃i=1nAi∣=∑i∣Ai∣−∑i<j∣Ai∩Aj∣+∑i<j<k∣Ai∩Aj∩Ak∣−⋯+(−1)n+1∣A1∩⋯∩An∣
Proof (for two sets). Every element of A1∪A2 is in A1 or A2 or both. Counting ∣A1∣+∣A2∣ counts elements in A1∩A2 twice, so we subtract ∣A1∩A2∣ once: ∣A1∪A2∣=∣A1∣+∣A2∣−∣A1∩A2∣.
For the general case, an element in exactly t of the sets is counted (1t)−(2t)+(3t)−⋯=1−(1−1)t=1 time, which is correct. ■
Worked Example. How many integers from 1 to 1000 are not divisible by 2, 3, or 5?
Let A2 = multiples of 2, A3 = multiples of 3, A5 = multiples of 5.
∣A2∣=500, ∣A3∣=333, ∣A5∣=200.
∣A2∩A3∣=166, ∣A2∩A5∣=100, ∣A3∩A5∣=66.
∣A2∩A3∩A5∣=33.
∣A2∪A3∪A5∣=500+333+200−166−100−66+33=734.
Not divisible by 2, 3, or 5: 1000−734=266. ■
Worked Example. How many integers from 1 to 500 are divisible by 3 or 7 but not by 5?
Solution
Let A3 = multiples of 3, A7 = multiples of 7, A5 = multiples of 5 in 1,…,500.
∣A3∣=⌊500/3⌋=166 ∣A7∣=⌊500/7⌋=71 ∣A5∣=⌊500/5⌋=100.
∣A3∩A7∣=⌊500/21⌋=23 ∣A3∩A5∣=⌊500/15⌋=33 ∣A7∩A5∣=⌊500/35⌋=14 ∣A3∩A7∩A5∣=⌊500/105⌋=4.
Divisible by 3 or 7: ∣A3∪A7∣=166+71−23=214.
Divisible by 3 or 7 and by 5: ∣(A3∪A7)∩A5∣=∣A3∩A5∣+∣A7∩A5∣−∣A3∩A7∩A5∣=33+14−4=43.
Divisible by 3 or 7 but not by 5: 214−43=171. ■
4.4 Stars and Bars
Theorem 4.4. The number of ways to distribute n identical objects into k distinct bins is (k−1n+k−1).
Proof. The problem is equivalent to placing k−1 dividers among n objects, giving (nn+k−1)=(k−1n+k−1) arrangements. ■
Worked Example. How many solutions does x1+x2+x3=15 have with xi≥1?
Solution
Substitute yi=xi−1≥0. Then y1+y2+y3=15−3=12 with yi≥0. By stars and bars: (3−112+3−1)=(214)=91. ■
Worked Example. How many solutions does x1+x2+x3+x4=20 have with xi≥0?
Solution
Directly by stars and bars: (4−120+4−1)=(323)=1771. ■
4.5 The Pigeonhole Principle
Theorem 4.5 (Pigeonhole Principle). If n objects are placed into k boxes and n>kThen at Least one box contains at least ⌈n/k⌉ objects.
Proof. If every box contained at most ⌈n/k⌉−1 objects, the total would be at most k(⌈n/k⌉−1)<k⋅n/k=nContradicting that there are n objects. ■
Worked Example. In a class of 400 students, at least how many were born in the same month?
Solution
There are 12 months (boxes) and 400 students (objects). By the pigeonhole principle, some month Has at least ⌈400/12⌉=⌈33.33…⌉=34 students.
Worked Example. Show that among any n+1 integers from 1,2,…,2nTwo of them Differ by exactly n.
Solution
Partition 1,2,…,2n into n pigeonholes: 1,n+1, 2,n+2, … n,2n. Each pair sums to n+(n+k)=2n+k… Let me rephrase.
Partition into 1,n+1, 2,n+2, …, n,2n. These are n disjoint sets. If we select n+1 integers from 1,…,2nBy the pigeonhole principle two must lie in the Same set i,n+iAnd their difference is (n+i)−i=n. ■
Worked Example. Prove that any sequence of n2+1 distinct real numbers contains a monotone (increasing or decreasing) subsequence of length n+1.
Solution
Let a1,a2,…,an2+1 be the sequence. For each aiLet di be the length of the Longest increasing subsequence starting at aiAnd ei the length of the longest decreasing Subsequence starting at ai.
Suppose for contradiction that every monotone subsequence has length at most n. Then 1≤di≤n and 1≤ei≤nSo there are at most n2 distinct ordered pairs (di,ei). Since we have n2+1 elements, by the pigeonhole principle two indices i<j Have (di,ei)=(dj,ej).
If ai<ajThen di≥dj+1 (append ai before the increasing subsequence starting At aj), contradicting di=dj.
If ai>ajThen ei≥ej+1Contradicting ei=ej.
Either way we have a contradiction. ■
Theorem 4.6 (Generalised Pigeonhole Principle). If n objects are placed into k boxes, then at Least one box contains at least ⌈n/k⌉ objects. Equivalently, if each box contains at most m objects, then the total number of objects is at most km.
Worked Example. A drawer contains red, blue, and yellow socks. How many socks must be drawn (without looking) to guarantee at least 4 socks of the same colour?
Solution
There are 3 colours (boxes). By the generalised pigeonhole principle, drawing n socks guarantees At least ⌈n/3⌉ of one colour. We need ⌈n/3⌉≥4So n/3>3Giving n≥10.
With 9 socks it is possible to have 3 of each colour (no colour reaches 4). With 10 socks, one Colour must have at least ⌈10/3⌉=4.
Worked Example. Prove that in any group of n people, there are at least two who have shaken Hands with the same number of people (within the group).
Solution
Each person can shake hands with between 0 and n−1 others, giving n possible values. But the Values 0 and n−1 cannot both occur (if someone shook no hands, no one shook everyone’s hand, And vice versa). So there are at most n−1 distinct handshake counts among n people. By the Pigeonhole principle, at least two people have the same count. ■
4.6 Catalan Numbers
The n-th Catalan number is
Cn=n+11(n2n)=(n+1)!n!(2n)!
The first few values: C0=1, C1=1, C2=2, C3=5, C4=14, C5=42.
Catalan numbers count:
- The number of valid (properly matched) sequences of n pairs of parentheses.
- The number of binary search trees with n nodes.
- The number of ways to triangulate a convex (n+2)-gon.
- The number of lattice paths from (0,0) to (n,n) that never go above the diagonal.
Recurrence. C0=1 and for n≥1:
Cn=∑i=0n−1CiCn−1−i
Worked Example. Verify C3=5 by listing all valid sequences of 3 pairs of parentheses.
Solution
The five valid sequences are: ((())), (()()), (())(), ()(()), ()()().
Checking: C3=41(36)=41⋅20=5. ✓
4.7 Generating Functions for Combinatorics
The ordinary generating function (OGF) of a sequence an is
G(x)=∑n=0∞anxn
Common generating functions:
| Sequence an | Generating function G(x) |
|---|
| 1 | 1−x1 |
| rn | 1−rx1 |
| (kn+k) | (1−x)k+11 |
| n | (1−x)2x |
| n2 | (1−x)3x(1+x) |
Key operations. If A(x) generates an and B(x) generates bn:
- A(x)+B(x) generates an+bn (choice between types).
- A(x)⋅B(x) generates cn where cn=∑i=0naibn−i (combining two choices).
Worked Example. Find the number of ways to select n coins from unlimited supplies of 1p, 2p, And 5p coins.
Solution
The generating function is
G(x) = \underbrace{(1 + x + x^2 + \cdots)}_{\mathrm{1p\; coins{}} \cdot \underbrace{(1 + x^2 + x^4 + \cdots)}_{\mathrm{2p\; coins{}} \cdot \underbrace{(1 + x^5 + x^{10} + \cdots)}_{\mathrm{5p\; coins{}}
=1−x1⋅1−x21⋅1−x51
The coefficient of xn in the expansion gives the number of ways. For example, expanding the First few terms: 1+x+2x2+2x3+3x4+4x5+⋯So there are 4 ways to make 5p (5×1p; 3×1p + 1×2p; 1×1p + 2×2p; 1×5p).