Data Structures & AlgorithmsModerateICPC regional · modular arithmetic

Problem: Which statement about Fermat's little theorem is correct?

  1. A. If a^(p-1) ≡ 1 (mod p) then p is always prime
  2. B. If p is prime and a is not divisible by p, then a^(p-1) ≡ 1 (mod p)
  3. C. It holds for every integer p, prime or composite
  4. D. It gives a^p ≡ 0 (mod p) for all a

Correct answer: B. If p is prime and a is not divisible by p, then a^(p-1) ≡ 1 (mod p)

The theorem needs p prime and gcd(a, p) = 1. The converse fails for Carmichael numbers and composite p, which is the trap.

More Coding Interview Problems questions

Exams that ask this topic