Editorial for AAAA 1 P4 - Minimex
Submitting an official solution before solving the problem yourself is a bannable offence.
Author:
Observations
First, we must determine which integers are in the set of achievable MEX values of the given grid.
In order for an integer to be in the set of achievable MEX values, there must exist a subgrid of the given grid whose MEX is equal to
. In other words, there must exist a subgrid which contains all the integers from
to
but must not include
.
Notice that if such a subgrid exists, then the minimal subgrid containing the integers to
will also be a valid subgrid. In other words, it is always optimal to consider the minimal subgrid containing the integers
to
, and checking if
falls outside of it.
Formally, if and
denote the row and column number of the integer
in the given grid, then the value
is in the set of achievable MEX values if and only if
Another way to interpret this condition is if when adding the elements in the grid in order of increasing value, is in the set of achievable MEX values if and only if the minimal rectangle containing the inserted elements is expanded when
is inserted.
Thus, we can compute whether or not each value belongs to the set of achievable MEX values in time.
Subtask 1
In this subtask, . Thus, the grid can be interpreted as an array of length
.
Using our initial observations, we can first determine which integers are in the set of achievable MEX values of the given grid.
To count the number of grids which have the same set of achievable MEX values, we consider counting the number of ways to build the permutation when adding the elements of the grid in order of increasing value.
We first initialize our answer as , as there is only one way to place the element
into an empty array. Then, we iterate over the values of
from
to
. When inserting the value
there are two cases:
is in the set of achievable MEX values.
- In this case,
must be to the left of all previously inserted values, or to the right of all previously inserted values.
- Thus, we multiply our answer by
.
- In this case,
is not in the set of achievable MEX values.
- In this case,
must not expand the subarray containing all previously inserted values.
- In other words,
must be placed in bewteen two previously inserted elements.
- Since
elements have already been inserted, there are
"gaps" where
can be placed.
- Thus, we multiply our answer by
.
- In this case,
If we modulo our answer variable by every time we multiply, this provides a
solution.
Time Complexity:
Subtask 2
This subtask was meant to reward solutions which performed an transition for all
values, as well as other suboptimal implementations of the full solution.
Time Complexity:
Subtask 3
For the full solution, we take advantage of the fact that there are at most achievable MEX values, since the minimal rectangle containing the values
can only expand at most
times. We can use combinatorics to skip over the calculations for values which are not in the set of achievable MEX values.
Let be the
achievable MEX value in increasing order with 0-indexing. In particular,
and
. (Ruling out the trivial case of
).
Let be the number of ways to place the values
such that all of them correctly belong or do not belong to the set of achievable MEX values and they all fit within a
grid.
The base case is , as there is only
way to place
into a
grid.
To compute , we must have already inserted the values
into a rectangle with dimensions smaller than
, so that when inserting
, the minimal rectangle containing the inserted values is expanded to dimensions
(and thus
would be in the set of achievable MEX values).
Let the dimensions of the minimal rectangle before inserting be
. By our recurrence, there are
ways to insert the values
such that all of them correctly belong or do not belong to the set of achievable MEX values and they all fit within a
grid.
So, we can multiply by the number of ways to insert
into a surrounding
grid, and sum over all valid
and
to compute
.
Now, let us compute the ways to insert just .
If and
, there are
ways, since
must be placed at one of the four "corners" to be able to expand the minimal rectangle in both dimensions.
If and
, there are
ways, since
must be placed strictly to the left/right of all previously inserted values, while its row can be arbitrary among the existing rows.
If and
, there are
ways, since
must be placed strictly above/below all previously inserted values, while its column can be arbitrary among the existing columns.
Finally, after inserting , we must insert
. There are
ways to do this because each value can fill in the remaining spots in the rectangle one by one, which is counted via a product of consecutive integers (the number of remaining spots decreases by one each time.
Overall,
The sums can be optimized with prefix sums, and the product can be computed using preprocessed factorials and modular inverse.
The answer can be found in , where
is the set of achievable MEX values.
Time Complexity:
Bonus
Author Commentary
Average Codeforces problem lol
Comments