Number Theory & Combinatorics
Count arrangements and choices when the answer is astronomically large: modular inverses, binomial coefficients, totients and inclusion–exclusion.
After this lesson you will be able to
- Explain why division does not work modulo a prime and what a modular inverse is, and compute one with Fermat's theorem or extended Euclid.
- Precompute factorials and inverse factorials to answer C(n, r) mod p in O(1) per query.
- Build Pascal's triangle for any modulus, and use the identity C(n, r) = C(n−1, r−1) + C(n−1, r).
- Compute Euler's totient and use inclusion–exclusion to count numbers with a divisibility property.
- Recognise counting problems (paths, arrangements, distributions, balanced brackets) and map them to a formula.
Before you start. Numerics is essential: remainders, gcd, primes and modular exponentiation. Bit Manipulation is not needed, except the inclusion–exclusion masks, which use the same idea.