O (n) solution
If we start with 1, 2, 4, or 6 stones, Ahe will always win, because he simply takes them all in the first turn.
If we start with 3, he Awill lose no matter what he does, because regardless of whether he takes 1 or 2, he Bwill take 2 or 1 next and win.
If we start with 5, he Awill win, taking 2 first, sending Bin the above case, where he will start with 3 stones.
7, A , 4, B 3.
8, A , : , B .
9, A 1 B 8, .
10, A 2 B 8 , .
, O(n): let win[i] = true if i stones are winnable for the first person to move
:
win[1] = win[2] = win[4] = win[5] = win[6] = true, win[3] = false
win[x > 6] = not (win[x - 6] and win[x - 4] and win[x - 2] and win[x - 1])
:
win[7] = not (win[1] and win[3] and win[5] and win[6])
= not (true and false and true and true)
= not false
= true
, . .
O (1)
, : , A , B , , , k - 6, k - 4, k - 2, k - 1 .
win , :
win[k] = false if k = 3, 8, 11, 16, 19, 24, 27, 32...
=> win[k] = false iff k mod 8 == 3 or k mod 8 == 0
99, 99 mod 8 = 3, A .