codeforces-scrape

Codeforces scraped problems and editorials
git clone git://git.laack.co/codeforces-scrape.git
Log | Files | Refs | README

1957C.md (3071B)


      1 Source: https://codeforces.com/problemset/problem/1957/C
      2 
      3 C. How Does the Rook Move?
      4 
      5 time limit per test
      6 
      7 2 seconds
      8 
      9 memory limit per test
     10 
     11 256 megabytes
     12 
     13 input
     14 
     15 standard input
     16 
     17 output
     18 
     19 standard output
     20 
     21 You are given an $n \times n$ chessboard where you and the computer take turns alternatingly to place white rooks & black rooks on the board respectively. While placing rooks, you have to ensure that no two rooks attack each other. Two rooks attack each other if they share the same row or column regardless of color.
     22 
     23 A valid move is placing a rook on a position ($r$, $c$) such that it doesn't attack any other rook.
     24 
     25 You start first, and when you make a valid move in your turn, placing a white rook at position ($r$, $c$), the computer will mirror you and place a black rook at position ($c$, $r$) in its turn. If $r = c$, then the computer can't mirror your move, and skips its turn.
     26 
     27 You have already played $k$ moves with the computer (the computer tries to mirror these moves too), and you must continue playing the game until there are no valid moves remaining. How many different final configurations are possible when you continue the game after the $k$ moves? It is guaranteed that the $k$ moves and the implied computer moves are valid. Since the answer may be large, print it modulo $10^9+7$.
     28 
     29 Two configurations are considered different if there exists a coordinate ($r$, $c$) which has a rook in one configuration, but not in the other or the color of the rook on the coordinate is different.
     30 
     31 Input
     32 
     33 The first line contains a single integer $t$ ($1 \leq t \leq 10^4$) — the number of test cases.
     34 
     35 The first line of each test case contains two integers $n$ and $k$ ($1 \leq n \leq 3 \cdot 10^5$, $0 \leq k \leq n$) — the size of the chessboard and the number of moves you have already played respectively.
     36 
     37 Each of the next $k$ lines of the test case contains two integers $r_i$ and $c_i$, denoting the $i$-th move you made.
     38 
     39 It is guaranteed that the $k$ moves and the implied computer moves are valid.
     40 
     41 It is guaranteed that the sum of $n$ over all test cases does not exceed $3 \cdot 10^5$.
     42 
     43 Output
     44 
     45 For each test case, output a single integer on a new line — the total number of possible final configurations modulo $10^9+7$.
     46 
     47 Example
     48 
     49 Input
     50 
     51 ```
     52 3
     53 
     54 4 1
     55 
     56 1 2
     57 
     58 8 1
     59 
     60 7 6
     61 
     62 1000 4
     63 
     64 4 4
     65 
     66 952 343
     67 
     68 222 333
     69 
     70 90 91
     71 ```
     72 
     73 Output
     74 
     75 ```
     76 3
     77 331
     78 671968183
     79 ```
     80 
     81 Note
     82 
     83 In the first test case, we have a $4 \times 4$ grid and you've already played $1$ move. After you and the computer play a turn, we have a white rook at ($1$, $2$), and a black rook at ($2$, $1$). There are three possible configurations from this state —
     84 
     85 1. You place a white rook at ($3$, $4$) and the computer places a black rook at ($4$, $3$) as a response.
     86 2. You place a white rook at ($4$, $3$) and the computer places a black rook at ($3$, $4$) as a response.
     87 3. You place a white rook at ($3$, $3$) and then at ($4$, $4$), or the other way around. They both result in the same configuration.
     88 
     89  ![](https://espresso.codeforces.com/393ca3da2a463c5f4157d901fb4ff2f4cbc9a101.png)