Editorial for TLE '16 Contest 3 P4 - Gaussian Elimination


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

For the first subtask, we can simply try all possible combinations of N and M. There are 9 combinations to check.

Time complexity: \mathcal{O}(1)

For the second subtask, we need to make several observations for this problem. First, when we perform a row or column reduction, we compress the entire grid into a new continuous grid after the move. This means that no matter which row we remove, the end state will be the same. The same applies for the columns. Thus, the only decision that matters is whether to perform a row or a column reduction.

Now we need to apply some game theory knowledge. Let dp[i][j] = 1 if the first person to play on a grid with size (i, j) will win, and 0 if the first person will lose. The base case we have to deal with is if i = 1 or j = 1, in this case, the first person to play always wins. Otherwise, we can see that the player to move will either make a grid with size (i, j-1) or size (i-1, j). If the person going first at size (i, j) wants to win, he should send the other person into a losing position. Thus, dp[i][j] = 1 if dp[i-1][j] = 0 or dp[i][j-1] = 0. Otherwise, dp[i][j] = 0. After generating the entire DP array, the answer is located at dp[N][M].

Time Complexity: \mathcal{O}(NM)

To solve the third subtask, we need to notice a pattern with the DP array since generating the entire DP array is too time-consuming. Again, if N = 1 or M = 1, we know that the answer to dp[N][M] will be 1. If we generate DP values for small grids, we realize that a checkerboard pattern is formed starting at (2,2). Thus, if N and M are both not equal to 1, the player who plays first will win if i+j \equiv 1 \pmod{2}.

Note: This solution can be proven correct with induction. The proof of correctness is left as an exercise.

Time Complexity: \mathcal{O}(1)


Comments

There are no comments at the moment.