Editorial for DMOPC '22 Contest 4 P2 - Fake Painting

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: yzhao123

After performing all brushstrokes, there are possible final orientations - not flipped, vertically flipped, horizontally flipped, and both vertically and horizontally flipped. Note that a vertical or horizontal flip can be performed without changing the values of the grid. For example, performing operations choosing the same cell with as , , and all horizontal flips will simply flip the grid horizontally. The same logic applies to vertical flips. Thus, any final orientation can be reached. That means we can check each possible final orientation, and if one of them works, the answer is yes.

We run the following check for each orientation: calculate a array which represents the differences between and . Let , , , and . Then, must hold. To see this clearly, draw out a chessboard with alternating white and black tiles. Whenever an operation is performed, is added to white tile and black tile. Thus, the sum of the black tiles must equal the sum of the white tiles.

We calculate the difference array with size this time. Note that each cell is only connected to cell , , and . Now, there are a few cases to consider.

• If and , this is the same as the case, and we can follow the same logic as above.
• If and , is the centre cell. Since can be arbitrarily added to the cell, must be even.
• If , then there are only cells to consider, and . We can arbitrarily add to either of the cells or add to both cells, which can achieve any combination of values such that is even. The same logic applies to the case with .

After trying all possible orientations, we have our answer.

Remarks

We actually only have to try final orientations, since the cases with no flip/both flips and horizontal flip/vertical flip have the same checks.