[AtCoder] B - Counting Grids

 

 

The key observation is that there is always at most 1 cell that violates both conditions. 

Proof: 

if x violates both conditions, that means all other numbers on the same column < x and all other nubmers on the same row > x, so we have u < x && v > x.

if there is another violating cell, by definition, it can not be on the same row or the same column with x, so have the following diagram. similarly we have u > y && v < y.

using x's info we have u < v; using y's info we have u > v, contradiction!

 

 

 

With the above observation, we iterate over all the possible violating numbers and for each such number X, we do the following calculation to get the number of invalid ways.

1. pick a cell, there are N * N possible spots.

2. For each spot, pick N - 1 numbers < X and get their permutation count; do the same for > X. 

3. For the remaining unfilled cells, there are (N * N - (N * 2 - 1))! possible ways.

4. multiply the results from step 1-3 to get the total number of invalid ways for a single violating number X.

 

Subtract the sum of all invalid ways from (N^2)! to get the final answer.

 

posted @ 2023-03-21 23:38  Review->Improve  阅读(9)  评论(0编辑  收藏  举报