AAAA 1 P4 - Minimex
View as PDF"too many constructive problems... can we have a math problem instead??"
– nobody
You are given an grid containing each of the integers from
to
exactly once. Denote the integer in the
row and
column as
.
For any grid of integers, define its set of achievable MEX values to be all integers for which there exists a subgrid of the grid whose MEX is equal to
.
Determine the number of grids containing each of the integers from
to
exactly once which have the same set of achievable MEX values as the given grid, modulo
.
Note: MEX means minimum excluded value. The MEX of a set of numbers refers to the smallest non-negative integer which is not contained in
.
Constraints
The given grid contains all integers from
to
exactly once.
Subtask 1 [20%]
Subtask 2 [40%]
Subtask 3 [40%]
No additional constraints.
Input Specification
The first line contains two integers, and
, the number of rows and the number of columns in the grid, respectively.
The next lines each contain
integers, where the
integer on the
line is
, describing the given grid.
Output Specification
Output one line containing one integer, the number of grids containing each of the integers from
to
exactly once which have the same set of achievable MEX values as the given grid, modulo
.
Sample Input 1
2 2
0 2
3 1
Sample Output 1
8
Explanation for Sample 1
The set of achievable MEX values of the given grid is . A MEX of
is achievable by taking any subgrid which does not contain
(e.g. the subgrid consisting only of the value
), a MEX of
is achievable by taking the subgrid consisting only of the value
, and a MEX of
is achievable by taking the subgrid consisting of the entire grid.
There are such
grids with the same set of achievable MEX values
, including the given grid. They are listed as follows:
Sample Input 2
3 4
8 10 11 9
7 6 3 4
5 0 2 1
Sample Output 2
261888
Explanation for Sample 2
The set of achievable MEX values of the given grid is . Let
denote the subgrid spanning rows
to
and columns
to
, numbered from
to
and
to
, respectively. The following is a list of the achievable MEX values followed by one of its possible corresponding subgrids.
:
:
:
:
:
:
It can be shown that no other MEX values are achievable. It can also be shown that there are exactly grids with
rows and
columns containing all of the numbers from
to
exactly once which have the set of achievable MEX values
, including the given grid.
Sample Input 3
1 20
0 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 1
Sample Output 3
321822771
Explanation for Sample 3
Remember to output the answer modulo .
Comments