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?
  1. A. [0, 1, 0, 1, 1, 0, 1]
  2. B. [0, 1, 0, 1, 2, 3, 2]
  3. C. [0, 1, 0, 1, 2, 0, 1]
  4. 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

Exams that ask this topic