Notes on the combinatorics part of CS2304: Mathematical Foundations of Computer Science.
Review
Textbook:
An invitation to discrete mathematic

Inclusion-Exclusion Principle
General formula:
proved by induction.
Derangements
Let D(n) denote the number of derangements of n elements.
Recurrence
D(n) = (n-1)[D(n-1) + D(n-2)]
initial condition: D(1) = 0, D(2) = 1
Closed form
or written as:
Derivation via the inclusion-exclusion principle
Let A_i denote the set of permutations in which “the i-th element is in its original position”. Then the number of derangements is , i.e. the number of permutations in which no element is in its original position.
Converting to the complement
- $|A{i_1} \cap A{i2} \cap … \cap A{i_k}|$ denotes the number of permutations in which exactly k elements are in their original positions
- There are
ways to choose these k positions - The remaining n-k positions can be permuted arbitrarily, giving
ways - Therefore
As n → ∞,
Euler’s totient function
Euler’s totient function
How to compute it
Multiplicativity: if a and b are coprime, then
Totient of a prime: if p is prime, then
Totient of a prime power: if p is prime and k ≥ 1, then
General formula: for any positive integer n, if the prime factorization of n is:
then by the inclusion-exclusion principle:
Generating Function
A generating function is a powerful tool that turns a sequence
where
Used for permutation and combination problems:
Used for the probability distribution of a discrete random variable:
3. Applications of generating functions
3.1 Solving recurrence relations
1 | # 示例:使用生成函数求解斐波那契数列 |
3.2 Counting problems
Example: how many ways are there to place
- Generating function:
- The coefficient of
in the expansion is the answer
3.3 Applications in probability
1 | # 使用生成函数计算两个骰子和的概率分布 |
4. Properties of generating functions
- Addition: adding sequences corresponds to adding generating functions
- Multiplication: convolving sequences corresponds to multiplying generating functions
- Differentiation:
- Integration:
5. Common generating functions
| Sequence | Generating function |
|---|---|
| Fibonacci sequence |
6. Example: computing binomial coefficients
1 | # 使用生成函数计算组合数 C(n,k) |
Translated from the Chinese original.

