Data Structures & AlgorithmsModerateCodeforces Div2 B · modular arithmetic
Problem: What does Fermat's little theorem let you compute efficiently?
- A. The number of divisors of a number
- B. Modular inverses via a^(p-2) mod p for prime p
- C. The shortest path between two nodes
- 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
- Problem: Which statement about the Bellman-Ford algorithm is correct when there is a negative cycle reachable from the source?
- Problem: What is the minimum number of colours needed to colour a bipartite graph so that no adjacent vertices share a colour?
- Problem: Which operation does the 'pointer jumping' technique speed up in parallel algorithms, and what is its complexity?
- Problem: Which problem asks for the number of ways to walk exactly k steps on a graph from u to v?
- Problem: What is the purpose of path compression in a union-find structure?
- Problem: Which structure answers 'number of points in a rectangle' queries offline in O(log n) each using sorting and a Fenwick tree?