Numerics & Math
The arithmetic toolkit of contests: remainders, gcd, primes, factorisation and modular arithmetic, each turning a huge computation into a few steps.
After this lesson you will be able to
- Use remainders (
%) to reason about divisibility, cycles and grouping, including with negative numbers. - Compute gcd with Euclid's algorithm and lcm from it, and explain why Euclid is fast.
- Test primality in O(√n), list primes with the sieve of Eratosthenes, and factorise a number.
- Do arithmetic modulo a prime, including fast modular exponentiation and inverses.
- Recognise when a counting problem is really about divisibility, and combine it with binary search and inclusion–exclusion.
Before you start. Recursion I, for the idea of a problem shrinking each step. You need to be comfortable with , and loops from Python Basics.