Data Structures & AlgorithmsModerateICPC regional · modular arithmetic
Problem: Which statement about Fermat's little theorem is correct?
- A. If a^(p-1) ≡ 1 (mod p) then p is always prime
- B. If p is prime and a is not divisible by p, then a^(p-1) ≡ 1 (mod p)
- C. It holds for every integer p, prime or composite
- 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
- Problem: What is the time for one point update and one rectangle-sum query in a 2D Fenwick tree on an n x n grid?
- Problem: Why can subtree queries on a rooted tree be answered with a range query on an array?
- Problem: An undirected graph has 10 vertices (0–9) and 6 edges: 0-1, 1-2, 3-4, 5-6, 6-7, 7-5. What is the minimum number of new edges that…
- Problem: In a graph with vertices 0–7 and edges 0-1, 1-2, 2-3, 3-0, 3-4, 4-5, 5-6, 6-4, 6-7, which edges lie on at least one cycle? How…
- Problem: In the undirected graph with edges 0-1, 0-2, 1-3, 2-3, 3-4, 4-5, what is the length of the shortest walk from 0 to 5 that uses an…
- Problem: A tree on nodes 1–9 has edges 1-2, 2-3, 3-4, 4-5, 5-6, 6-7, 3-8, 8-9. The eccentricity of a node is its largest distance to any…