Official

E - Chmin XOR Game Editorial by evima


Let’s experiment with \(N=2\). The results for \(0 \le A_1,A_2 < 20\) are as follows. \(1\) means the first player wins, and \(0\) means the second player wins.

01111111111111111111
11001111111111111111
10101111111111111111
10011111111111111111
11111111011101111111
11111111110011001111
11111111101010101111
11111111100110011111
11110111111101111111
11111100111111001111
11111010111110101111
11111001111110011111
11110111011111111111
11111100110011111111
11111010101011111111
11111001100111111111
11111111111111111111
11111111111111111111
11111111111111111111
11111111111111111111

You can see a fractal structure. From this, we can guess that base-\(4\) numbers and so on are involved in the winning condition. This guess turns out to be correct.

For the first player to lose in the above case, one of the following must be met.

  • Both \(A_1\) and \(A_2\) are at most \(3\), and they are both zero or both at least \(1\) and different.
  • Both \(A_1\) and \(A_2\) are at least \(4\), the quotients of them divided by \(4\) are different, and the remainders of them divided by \(4\) are both \(0\) or both at least \(1\) and different.

Let’s look at the elements of \(A\) in base \(4\). For example, if \(A=(5,8)\), we see it as \(((1,1),(0,2))\). From the above, we can guess the following winning condition for \(N=2\):

  • there is an integer \(j\) such that “exactly one of \(A_{1,j}\) and \(A_{2,j}\) is \(0\), or both of them are at least \(1\) and equal.”

Let’s prove this. First, we show that if the winning condition is met, one can give the opponent a board that does not satisfy the winning condition. From now on, we simply call the condition inside the quotation marks “the condition.”

We take the maximum \(j\) that meets the condition. Then, one should operate with the following non-negative integer \(X\).

  • If \(0 < A_{1,j} = A_{2,j}\):
    • If the \(k\)-th digit meets the condition, let the \(k\)-th digit of \(X\) be \(\max(A_{1,k},A_{2,k})\).
    • Otherwise, let the \(k\)-th digit of \(X\) be \(0\).
  • If \(0 = A_{1,j} < A_{2,j}\):
    • If the \(k\)-th digit meets the condition and \(0 < A_{1,k}=A_{2,k}\), let the \(k\)-th digit of \(X\) be \(0\).
    • If the \(k\)-th digit meets the condition and \(0 = A_{1,k}<A_{2,k}\), let the \(k\)-th digit of \(X\) be \(A_{2,k}\).
    • If the \(k\)-th digit meets the condition and \(0 = A_{2,k}<A_{1,k}\), let the \(k\)-th digit of \(X\) be anything other than \(0,A_{1,k}\).
    • If the \(k\)-th digit does not meet the condition, let the \(k\)-th digit of \(X\) be \(0\).

Let’s prove that if the winning condition is not met, no matter how one operates, she or he will give the opponent a board that meets the winning condition. Let the highest digit of \(X\) be the \(k\)-th digit. Then, \((A_{1,k},A_{2,k})\) is one of \((0,0),(1,2),(2,3),(3,1)\), and it can be verified that no matter which of \(0,1,2,3\) one chooses as the \(k\)-th digit of \(X\) in the operation, the \(k\)-th digit will meet the condition.

Therefore, it has been proved that the winning condition for \(N=2\) is the above. This solution can be applied to the general \(N\) by extending it as follows:

  • there is an integer \(j\) such that “exactly one of \(1,2,3\) appears in \(A_{1,j},A_{2,j},\dots,A_{N,j}\).”

It can be proved by slightly modifying the above, so we omit it. In proving it, note that there are more possible sets of integers that exist as the \(k\)-th digit.

Therefore, by implementing the above, you can solve the problem in \(\mathrm{O}(N\log A)\).

(Modified the first draft written by GPT-4.)

posted:
last update: