Math and bits

Modular arithmetic, fast power, gcd and the sieve, then bit operations and subset masks.

About 4 hours of study

Learning objectives

Lessons

  1. 1

    Math for contests

    Modular arithmetic, fast power, gcd and lcm, and a sieve with vector<bool>: the number theory every contestant uses.

  2. 2

    Bit tricks and subset masks Intermediate

    Test, set and clear bits, use xor to cancel pairs, count one bits, and walk through every subset of a small set with a mask.

Module practice

Module quiz

1. In C++, what is -2 % 7?
2. What is gcd(12, 18)?
3. How do you compute x / y modulo a prime p?
4. What does x & (x - 1) do?
5. Every number in a list appears twice except one. What finds it in O(n) with no extra memory?
6. n = 20 items. About how many subsets does a mask loop visit?

‹ Search, greedy and DPGraph algorithms ›