Data Structures & AlgorithmsVery hardICPC regional · modular arithmetic
Problem: How many integers x with 1 <= x <= 100 satisfy x^3 ≡ x (mod 7)?
Problem: How many integers x with 1 <= x <= 100 satisfy x^3 ≡ x (mod 7)?
print(sum(1 for x in range(1, 101) if pow(x, 3, 7) == x % 7))What is printed?
- A. 44
- B. 43
- C. 29
- D. 100
Correct answer: B. 43
x³ ≡ x (mod 7) only for residues 0, 1 and 6 (cubes mod 7 are 0 or ±1), and [1, 100] holds 14 + 15 + 14 = 43 such numbers. Starting the range at 0 gives 44, forgetting residue 0 (x³ − x = x(x − 1)(x + 1)) gives 29, and confusing this with Fermat's x⁷ ≡ x gives 100.
More Coding Interview Problems questions
- Problem: What does the following print? python from math import lcm print(lcm(range(1, 11)))
- Problem: How many integers from 1 to 100 have exactly 3 positive divisors? python print(sum(1 for x in range(1, 101) if sum(1 for d in…
- Problem: Piles of stones are 3, 4 and 5. Two players alternately remove any positive number of stones from one pile, and whoever takes the…
- Problem: A subtraction game lets a player remove 1, 3 or 4 stones. Let g(n) be the Grundy number (mex of the Grundy values reachable in…
- Problem: Two fair six-sided dice are rolled. What is the expected value of the larger of the two faces (the maximum)?
- Problem: Words apple, app, apt, bat, banana are stored in a trie that keeps a count of words passing through each node. What does the code…