Data Structures & AlgorithmsVery hardICPC regional · game theory
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 one move, with g(0) = 0). Run the code and print g(0) to g(6).
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 one move, with g(0) = 0). Run the code and print g(0) to g(6).
g = [0] * 7
for n in range(1, 7):
reach = {g[n - s] for s in (1, 3, 4) if n - s >= 0}
m = 0
while m in reach:
m += 1
g[n] = m
print(g)What is printed?- A. [0, 1, 0, 1, 1, 0, 1]
- B. [0, 1, 0, 1, 2, 3, 2]
- C. [0, 1, 0, 1, 2, 0, 1]
- D. [0, 1, 1, 0, 1, 1, 0]
Correct answer: B. [0, 1, 0, 1, 2, 3, 2]
Grundy values follow from mex: g(1)=1, g(2)=0, g(3)=1, g(4)=2, g(5)=3 and g(6)=2. Players lose from positions with Grundy value 0, so the table is needed to see which start positions are losing.
More Coding Interview Problems questions
- 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…
- Problem: The longest subarray with equally many 0s and 1s of a = [0, 1, 0, 0, 1, 1, 0] is needed. Run the prefix-balance code and print…
- Problem: Find the 3 smallest pair sums from A = [1, 7, 11] and B = [2, 4, 6], using a heap that starts with each A element paired with…
- Problem: An undirected connected graph has an Euler circuit (a closed walk using every edge exactly once) if and only if which condition…
- Problem: Using 2D prefix sums on M = [[1,2,3],[4,5,6],[7,8,9]], what is the sum of the rectangle with rows 1..2 and columns 1..2…