Data Structures & AlgorithmsHardICPC regional · game theory
Problem: Grundy numbers for the subtraction game where a move removes 1 or 2 stones: what are g(0) through g(5)? Run the code.
Problem: Grundy numbers for the subtraction game where a move removes 1 or 2 stones: what are g(0) through g(5)? Run the code.
g = [0] * 6
for n in range(1, 6):
reach = {g[n - s] for s in (1, 2) if n - s >= 0}
m = 0
while m in reach:
m += 1
g[n] = m
print(g)What is printed?- A. [0, 1, 2, 0, 1, 2]
- B. [0, 1, 2, 1, 0, 2]
- C. [0, 1, 0, 1, 0, 1]
- D. [0, 1, 2, 3, 4, 5]
Correct answer: A. [0, 1, 2, 0, 1, 2]
The Grundy values repeat with period 3: g(n) = n mod 3. Second player wins exactly when n is divisible by 3.
More Coding Interview Problems questions
- Problem: Using the bubble sort rule, what is the array after exactly one left-to-right pass over [3, 1, 2]? Run the code. python a = [3…
- Problem: How many legal moves does a knight have from the corner (0,0) of an 8 x 8 board? Run the code. python moves = [(1, 2), (2, 1)…
- Problem: What is 7^222 mod 10? Run the code. python print(pow(7, 222, 10)) What is printed?
- Problem: How many integers in [1, 500] are divisible by 4 or by 6? Run the code. python print(sum(1 for x in range(1, 501) if x % 4 == 0…
- Problem: How many ways can [1, 2, 3, 4] be split into two non-empty contiguous parts with equal sums? Run the code. python a = [1, 2, 3…
- Problem: Which algorithm finds all articulation points (cut vertices) of an undirected graph in O(V + E) time using DFS low-link values?