Editorial for Yet Another Contest 9 P4 - Grid Push
Submitting an official solution before solving the problem yourself is a bannable offence.
Author:
Subtask 1 Hint
How many unique grids exist such that the last operation used was of type ? What about type ? Do the cases ever overlap?Subtask 2 Hint
In this subtask, the cases from Subtask do overlap. Think very carefully about when this happens.Subtask 3 Hint
The dp transition should look very familiar. Does it remind you of any well-known structure?Full Solution
We'll start by introducing some notation. Let represent the subgrid of which spans rows to and columns to . represents an empty grid if or .
Consider the following dp state:
As our base case, if or , then is an empty grid and there is only one way to fill it, so . In all other cases, we have .
Suppose that we are considering the state . Note that, intuitively, we only care about the prefix of and of , since any other elements would never appear in .
Suppose that the last operation was of type . The column of is fixed, so the number of ways to fill would be equal to . Similarly, if the last operation was of type , then the row of is fixed, so the number of ways to fill would be equal to .
If there were no overlap between the two cases, then we would have . We claim that the only time that the overlap does happen, we also have , so we can write .
Proof of Claim
Suppose that the last operation could be either of type or type and we would get the same grid for . Consider the sequence of operations used to create the grid. Suppose that some positive number of type operations were performed after a type operation. Then the first row of would have values for some (possibly ). Equating this sequence elementwise with the sequence (which we get if the last operation was of type ) gives us for all . We can apply the same logic to get for all , and we are done.We can optimize this transition by noticing that it is nearly identical to the one used in creating Pascal's triangle, and therefore, we can apply binomial coefficients. The details are left as an exercise for the reader.
Time Complexity:
Comments