D - Choose Your Role 解説
by
PinkRabbit
印刷ページ
Another Solution
We choose to play as the second player. Our strategy against the first player is as follows.
Label the \(j\)-th stone in the \(i\)-th pile as \((i, j)\), so that every stone receives a distinct label \((i, j) \in [N] \times [N - 1]\).
Define an involution \(S\) on \([N] \times [N - 1]\) by
\[ S(i, j) = \begin{cases} (j, i - 1) \text{,} & \text{if\ } j < i \text{,} \\ (j + 1, i) \text{,} & \text{if\ } j \ge i \text{.} \end{cases} \]
It is easy to check that \(S \circ S = \operatorname{id}\) and that \(S\) has no fixed points, so \(S\) partitions all stones into pairs. The involution is illustrated below.

Here, \(S\) pairs stones lying at corresponding positions in the two triangles.
An important fact is that if \(S(i_1, j_1) = (i_2, j_2)\), then \(i_1 \ne i_2\); equivalently, \(S\) never maps a stone to a stone in the same pile. Another important fact is that, for a fixed pile \(i\), if \(j_1 \ne j_2\), then the first coordinates of \(S(i, j_1)\) and \(S(i, j_2)\) are different.
As the second player, we maintain the following invariant:
Each stone \(x\) is still present if and only if \(S(x)\) is still present.
In other words, the remaining stones always form a union of \(S\)-pairs. The invariant holds initially.
Suppose the first player removes \(k\) (\(1 \le k \le K\)) stones from pile \(i\). Since stones in the same pile are indistinguishable, we may label the removed stones as
\[ (i, j_1), \ldots, (i, j_k) \text{,} \]
where these are any \(k\) labels that were present in pile \(i\) before the move. Then their partners
\[ S(i, j_1), \ldots, S(i, j_k) \]
lie in distinct piles, none of which is pile \(i\). By the invariant, these partner stones are also still present.
Therefore, removing one stone from each of those \(k\) piles is a legal move for the second player. We output these piles. This restores the invariant, so we can always respond to the first player’s move.
Hence the first player eventually becomes unable to move, and we win.
Implementation (C++), running in \(O(N^3)\) time and easily optimizable.
NB. This solution is essentially the same as the official editorial if we lift the left triangle up by 1 unit and read the second coordinates of the labels as colors.
投稿日時:
最終更新:
