Official

B - Uniformly Distributed Editorial by evima


Consider any \(2 \times 2\) submatrix in the grid.
Let \((i, j), (i + 1, j), (i, j + 1), (i + 1, j + 1)\) be the squares in it, and consider the following two sequences of moves:

  • \((i, j) \rightarrow (i, j + 1) \rightarrow(i + 1, j + 1)\)
  • \((i, j) \rightarrow (i + 1, j) \rightarrow(i + 1, j + 1)\)

For each of them, let us think of a way to get from \((1, 1)\) to \((H, W)\) using it. Here, we arbitrarily choose the paths from \((1, 1)\) to \((i, j)\) and from \((i + 1, j + 1)\) to \((H, W)\), but we just make sure we use the same ones. Then, we can see that \((i, j + 1)\) and \((i + 1, j)\) must have the same color.
By applying it on all \(2 \times 2\) submatrices, we obtain the following fact:

  • All squares \((i, j)\) with the same sum \(i + j\) must have the same color.

One move increases \(i + j\) by \(1\), so we never visit multiple squares with the same sum \(i + j\). Thus, the above condition is not just a necessary condition; it is a sufficient condition of the one in the statement.
Thus, the answer is the product of the following over all sets of squares with the same sum \(i + j\):

  • \(2\) if they are all .;
  • \(0\) if they contain both R and B (contradiction);
  • \(1\) if none of the above holds.

posted:
last update: