Data Structures & AlgorithmsModerateCodeforces Div2 B · modular arithmetic

Problem: What does Fermat's little theorem let you compute efficiently?

  1. A. The number of divisors of a number
  2. B. Modular inverses via a^(p-2) mod p for prime p
  3. C. The shortest path between two nodes
  4. D. The median of a stream

Correct answer: B. Modular inverses via a^(p-2) mod p for prime p

For a prime p, a^(p-2) is the inverse of a modulo p, so exponentiation computes it in O(log p).

More Coding Interview Problems questions

Exams that ask this topic