Official

B - Torus Loop Editorial by evima


Let \(C_{i,j}\) be \(1\) if the tile in cell \((i,j)\) has a segment whose endpoint is the midpoint of its left edge, and \(0\) otherwise. Once \(C_{i,0}\) is fixed, all of \(C_{i,1}, C_{i,2}, \ldots,C_{i,W-1}\) are determined uniquely. Specifically, \(C_{i,j+1}=1-C_{i,j}\) if \(S_{i,j}\) is A, and \(C_{i,j+1}=C_{i,j}\) if \(S_{i,j}\) is B. However, if the number of As among \(C_{i,1}, C_{i,2}, \ldots,C_{i,W-1}\) is odd, no valid placement exists.

Performing the same reasoning for columns, it seems there are \(2^{H+W}\) placements, but for any cell with \(S_{i,j} =\) B the four surrounding midpoints cannot be all \(0\) or all \(1\). Thus, once the binary pattern of row \(i\) is fixed, the binary pattern of column \(j\) is also determined. Model these conditions as a bipartite graph; if it is consistent, let \(S\) be the number of connected components of that graph, and the answer is \(2^{S}\).

posted:
last update: