A10664 | Complete the Permutations
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
ZS the Coder is given two permutations $p$ and $q$ of ${1,2,...,n}$ , but some of their elements are replaced with $0$ . The distance between two permutations $p$ and $q$ is defined as the minimum number of moves required to turn $p$ into $q$ . A move consists of swapping exactly $2$ elements of $p$ .
ZS the Coder wants to determine the number of ways to replace the zeros with positive integers from the set ${1,2,...,n}$ such that $p$ and $q$ are permutations of ${1,2,...,n}$ and the distance between $p$ and $q$ is exactly $k$ .
ZS the Coder wants to find the answer for all $0<=k<=n-1$ . Can you help him?
ZS the Coder wants to determine the number of ways to replace the zeros with positive integers from the set ${1,2,...,n}$ such that $p$ and $q$ are permutations of ${1,2,...,n}$ and the distance between $p$ and $q$ is exactly $k$ .
ZS the Coder wants to find the answer for all $0<=k<=n-1$ . Can you help him?
输入格式
The first line of the input contains a single integer $n$ ( $1<=n<=250$ ) — the number of elements in the permutations.
The second line contains $n$ integers, $p_{1},p_{2},...,p_{n}$ ( $0<=p_{i}<=n$ ) — the permutation $p$ . It is guaranteed that there is at least one way to replace zeros such that $p$ is a permutation of ${1,2,...,n}$ .
The third line contains $n$ integers, $q_{1},q_{2},...,q_{n}$ ( $0<=q_{i}<=n$ ) — the permutation $q$ . It is guaranteed that there is at least one way to replace zeros such that $q$ is a permutation of ${1,2,...,n}$ .
The second line contains $n$ integers, $p_{1},p_{2},...,p_{n}$ ( $0<=p_{i}<=n$ ) — the permutation $p$ . It is guaranteed that there is at least one way to replace zeros such that $p$ is a permutation of ${1,2,...,n}$ .
The third line contains $n$ integers, $q_{1},q_{2},...,q_{n}$ ( $0<=q_{i}<=n$ ) — the permutation $q$ . It is guaranteed that there is at least one way to replace zeros such that $q$ is a permutation of ${1,2,...,n}$ .
输出格式
Print $n$ integers, $i$ -th of them should denote the answer for $k=i-1$ . Since the answer may be quite large, and ZS the Coder loves weird primes, print them modulo $998244353=2^{23}·7·17+1$ , which is a prime.
输入输出样例
输入 #1
3 1 0 0 0 2 0
输出 #1
1 2 1
输入 #2
4 1 0 0 3 0 0 0 4
输出 #2
0 2 6 4
输入 #3
6 1 3 2 5 4 6 6 4 5 1 0 0
输出 #3
0 0 0 0 1 1
输入 #4
4 1 2 3 4 2 3 4 1
输出 #4
0 0 0 1
In the first sample case, there is the only way to replace zeros so that it takes $0$ swaps to convert $p$ into $q$ , namely $p=(1,2,3),q=(1,2,3)$ .
There are two ways to replace zeros so that it takes $1$ swap to turn $p$ into $q$ . One of these ways is $p=(1,2,3),q=(3,2,1)$ , then swapping $1$ and $3$ from $p$ transform it into $q$ . The other way is $p=(1,3,2),q=(1,2,3)$ . Swapping $2$ and $3$ works in this case.
Finally, there is one way to replace zeros so that it takes $2$ swaps to turn $p$ into $q$ , namely $p=(1,3,2),q=(3,2,1)$ . Then, we can transform $p$ into $q$ like following: .
There are two ways to replace zeros so that it takes $1$ swap to turn $p$ into $q$ . One of these ways is $p=(1,2,3),q=(3,2,1)$ , then swapping $1$ and $3$ from $p$ transform it into $q$ . The other way is $p=(1,3,2),q=(1,2,3)$ . Swapping $2$ and $3$ works in this case.
Finally, there is one way to replace zeros so that it takes $2$ swaps to turn $p$ into $q$ , namely $p=(1,3,2),q=(3,2,1)$ . Then, we can transform $p$ into $q$ like following: .
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted