原文
Calculate the number of ways to place rooks on chessboard so that both following conditions are met:
- each empty cell is under attack;
- exactly pairs of rooks attack each other.
An empty cell is under attack if there is at least one rook in the same row or at least one rook in the same column. Two rooks attack each other if they share the same row or column, and there are no other rooks between them. For example, there are only two pairs of rooks that attack each other in the following picture:
One of the ways to place the rooks for and
Two ways to place the rooks are considered different if there exists at least one cell which is empty in one of the ways but contains a rook in another way.
The answer might be large, so print it modulo 998244353.
Input
The only line of the input contains two integers and (; ).
Output
Print one integer — the number of ways to place the rooks, taken modulo 998244353.
Examples
input
1 | 3 2 |
output
1 | 6 |
input
1 | 3 3 |
output
1 | 0 |
input
1 | 4 0 |
output
1 | 24 |
input
1 | 1337 42 |
output
1 | 807905441 |
题意
rook可以对同一行和同一列攻击,提供两个数和,求在的棋盘上放置个rook使得:
- 所有棋盘格都可以被攻击到
- 有对rook可以互相供给
的放置方法总数。
思路
由于所有地方都需要被攻击到,那么就相当于一定是每个行都有一个rook或者每个列都有一个rook.本质上是一个排列组合问题,可以发现,行和列实际上是对称的,所以只需要考虑行列当中的一种。
假设每个行上都有一个rook,如果,那么必然是个rook分布在个列上。对应的排列方法一共有:
从上面的分析也可以发现,如果,那么不存在符合的情况。再注意当的时候需要,需要乘2,而的时候直接作为结果就可以了。
代码
1 |
|