## Editorial for Yet Another Contest 8 P4 - Fluke 2

Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.
Submitting an official solution before solving the problem yourself is a bannable offence.

Author: Josh

If , then Josh should go second, using the strategy described in Sample Interaction .

Otherwise, . We can show that Josh can force a win if he goes first, according to the following strategy:

• First, toggle any cell. After Nils responds, there will be two black cells.
• Then, repeatedly until Josh wins, check if there are two adjacent black cells. If so, toggle an adjacent cell to form a line of three. Otherwise, pick any cell between the two closest black cells, and toggle it black.

Let be the minimum distance between any two black cells. Because Nils can't undo Josh's last move, it is easy to show that decreases after each pair of turns. Once , Josh can immediately force a win. This proves that this strategy is correct.

Implemented naively, this solution will only score of the points, because it can take up to pairs of turns for to reach . However, if we always toggle the cell in the middle of the two closest black cells, is reduced to at most after each pair of turns. This is sufficient for of the points.

Let's consider . It's fairly easy to adapt our subtask solution to work in two dimensions:

• First, toggle any cell. After Nils responds, there will be two black cells.
• If these two cells are in different rows and columns, toggle any cell which shares a row or column with both cells. After Nils responds, there will be at least two black cells in the same row or column.
• Then, repeatedly until Josh wins, check if there are two adjacent black cells. If so, toggle an adjacent cell to form a line of three. Otherwise, pick any cell between the two closest black cells in the same row or column, and toggle it black.

Here, we instead define as the minimum distance between any two black cells in the same row or column. The correctness of this strategy is very similar to the proof in subtask .

Note that for full points, the first cell toggled should be in the middle of the grid, to minimize the starting value of .

The final case is where . It can be shown that Josh can win if he chooses to go second, since it is always possible to force the invariant that after Josh's turn, there should be exactly one black cell in each row.