Chessboard
Time limit1sMemory limit128 MB
Count permutations of 1..n whose i-th rook avoids row i and column i, modulo m.
- Level
Hard8 of 10
- Topics
- Combinatorics, Dynamic programming, Math
- Solved
- No attempts yet
Problem
Byteasar loves chess puzzles. He has subscribed to a chess magazine for years, and its latest issue features the following puzzle.
You are given a chessboard of size . In how many ways can you place rooks on the board so that no two rooks attack each other (that is, no two share a row or a column) and, for every , the -th rook lies in neither the -th row nor the -th column? The rooks, the rows, and the columns are all numbered from to . Report the answer modulo .
Puzzles like "mate in 13 moves" are a piece of cake for Byteasar, but this new kind of puzzle looks very hard to him. Could you help him?
Input
The first line contains two integers and separated by a space (, ).
Output
Print, on a single line, the number of valid rook placements modulo .