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 typeSubtask 2 Hint
In this subtask, the cases from SubtaskSubtask 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 typeWe 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