Official

E - Modulo Nim Editorial by evima


First, let us consider the problem to determine the winner for a given \(a\).

Since \(0\) or multiple occurrences of the same number do not affect the answer, the board can be represented as a set of natural numbers.

Let \(S\) be the current board. If \(S=\{1\}\) or \(S=\{2\}\), it is obvious that the current player loses.

Let us consider the other cases. Below, we assume \(\max(S) \geq 3\).

If the board has an odd number, a move with \(m=2\) wins the game, so all elements of \(S\) need to be even for the board to be losing. Similarly, if the board has a number that leaves a remainder of \(2\) when divided by \(4\), a move with \(m=4\) wins the game, so all elements of \(S\) need to be multiples of \(4\) for the board to be losing.

Additionally, by considering the move with \(m=3\), it can be seen that the board needs to satisfy one of the following to be losing.

  • \(S\) contains both a number that leaves a remainder of \(1\) and a number that leaves a remainder of \(2\) when divided by \(3\).
  • All elements of \(S\) are multiples of \(3\).

For the former case, the only losing board is \(\{4,8\}\). Otherwise, a move with \(m=12\) wins the game.

For the latter case, all elements of \(S\) need to be multiples of \(12\). Since \(a_i \leq 200\), there are at most \(2^{\lfloor \frac{200}{12} \rfloor}\) boards that have to be considered.

Because of this small number of relevant boards, we can list all losing \(S\) by determining the outcomes for these boards by methods such as memoized recursion.

Now, let us return to the original problem. The observations so far have revealed that Taro The First only loses in quite limited cases, which can be counted by methods such as bitmask DP.

posted:
last update: