Official

E - Rookhopper's Tour Editorial by evima


Let’s consider the conditions that the configuration of white stones must satisfy for victory.
First, since there cannot be more than one white stone in the same column, the black stone must move in one of the following patterns: (a) vertically -> horizontally -> vertically -> … -> vertically -> horizontally, or (b) horizontally -> vertically -> horizontally -> … -> horizontally -> vertically. Thus, \(M\) must be even.

Furthermore, it can be proven that for a configuration that satisfies the conditions, only one type of movement (a) or (b) is possible.

(Proof) Assume a configuration allows both types of movements (a) and (b). From the nature of the movements, we can confirm the following facts:

  • The cell the black stone enters in the \(i\)-th move in (a) and the cell it enters in the \((M-i)\)-th move in (b) are the same.
  • The white stone removed in the \(i\)-th move in (a) and the white stone removed in the \((M+1-i)\)-th move in (b) are the same.

From these facts, it can be confirmed that for \(1 \leq i < M\), the cell the black stone enters in the \(i\)-th move in (a) and the cell it enters in the \((i + 1)\)-th move in (a) can be traveled in both directions by jumping over a white stone. Thus, these two cells must be at a distance of 2. Therefore, all moves must travel a distance of 2, but a configuration that only allows moves traveling a distance of 2 contradicts the property that at most one white stone can be placed in each row and column. Hence, it is never the case that both types of movements (a) and (b) are possible. (End of proof)

The above consideration reveals that each configuration corresponds to either movement (a) or (b), so we just need to count the movements for each type.
Hereafter, we consider configurations where movement (a) is possible. (The same applies to (b).)

Let’s consider the properties of the configuration. For example, if the first move is downwards and the second move is to the right, then these moves look as follows:

  • Jump over the white stone at \((x,B)\) to land at \((x+1,B)\), then jump over the white stone at \((x+1,y)\) to land at \((x+1,y+1)\).

This means that stones are placed in both the \(x\)-th and \((x+1)\)-th rows.
Similarly, in general, the following relationship holds (where the move number \(n\) is considered modulo \(M\)):

  • Let \(n\) be odd. The white stones removed in the \(n\)-th and \((n+1)\)-th moves are placed in adjacent rows. Also, if the black stone comes from above, the stone in the \(n\)-th move is in a higher row than the one in the \((n+1)\)-th move; if it comes from below, it is in a lower row. \(\cdots(\ast)\)

A similar relationship holds for columns when \(n\) is even. Therefore, the placement of stones can be interpreted as an operation that alternately occupies two rows and two columns.

Based on these properties, we perform the counting. For simplicity, let’s first consider the case without the condition that the starting (and ending) point is \((A, B)\).
All configurations can be represented as operations that alternately occupy two rows and two columns. Conversely, if we fix the order of the rows and columns to be occupied, the corresponding starting point and the configuration of white stones are uniquely determined. Such a one-to-one correspondence allows the problem to be reduced to counting the order of the rows and columns to be occupied. The number of ways to order the rows and columns can be expressed in the form of binomial coefficients and thus can be counted.

Adding the condition that the starting point is \((A,B)\) imposes the following conditions on how rows and columns are occupied:

  • The white stones removed in the \((M-1)\)-th and \(M\)-th moves occupy either the \(A\)-th and \((A+1)\)-th rows, or the \((A-1)\)-th and \(A\)-th rows (which rows are occupied depends on whether the position of the white stone removed in the \((M-2)\)-th move is above or below the \(A\)-th row).
  • The white stones removed in the \(M\)-th and \(1\)-st moves occupy either the \(B\)-th and \((B+1)\)-th columns, or the \((B-1)\)-th and \(B\)-th columns (which columns are occupied depends on whether the position of the white stone removed in the \((M-1)\)-th move is to the right or left of the \(B\)-th column).

The problem asks us to count the ways to occupy rows and columns to satisfy these conditions. This can be done by fixing the relationship between the white stone removed in the \((M-2)\)-th move and the \(A\)-th row and the relationship between the white stone removed in the \((M-1)\)-th move and the \(B\)-th column, for a total of \(2 \times 2 = 4\) cases, after which the count can be expressed as the sum of binomial coefficients, so we can sum up all these counts to find the answer.
Proper implementation of the above solves the problem in \(\mathrm{O}(N)\).

posted:
last update: