A10537 | Coprime Permutation
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
Two positive integers are coprime if and only if they don't have a common divisor greater than $1$ .
Some bear doesn't want to tell Radewoosh how to solve some algorithmic problem. So, Radewoosh is going to break into that bear's safe with solutions. To pass through the door, he must enter a permutation of numbers $1$ through $n$ . The door opens if and only if an entered permutation $p_{1},p_{2},...,p_{n}$ satisfies:
In other words, two different elements are coprime if and only if their indices are coprime.
Some elements of a permutation may be already fixed. In how many ways can Radewoosh fill the remaining gaps so that the door will open? Print the answer modulo $10^{9}+7$ .
Some bear doesn't want to tell Radewoosh how to solve some algorithmic problem. So, Radewoosh is going to break into that bear's safe with solutions. To pass through the door, he must enter a permutation of numbers $1$ through $n$ . The door opens if and only if an entered permutation $p_{1},p_{2},...,p_{n}$ satisfies:
In other words, two different elements are coprime if and only if their indices are coprime.
Some elements of a permutation may be already fixed. In how many ways can Radewoosh fill the remaining gaps so that the door will open? Print the answer modulo $10^{9}+7$ .
输入格式
The first line of the input contains one integer $n$ ( $2<=n<=1000000$ ).
The second line contains $n$ integers $p_{1},p_{2},...,p_{n}$ ( $0<=p_{i}<=n$ ) where $p_{i}=0$ means a gap to fill, and $p_{i}>=1$ means a fixed number.
It's guaranteed that if $i≠j$ and $p_{i},p_{j}>=1$ then $p_{i}≠p_{j}$ .
The second line contains $n$ integers $p_{1},p_{2},...,p_{n}$ ( $0<=p_{i}<=n$ ) where $p_{i}=0$ means a gap to fill, and $p_{i}>=1$ means a fixed number.
It's guaranteed that if $i≠j$ and $p_{i},p_{j}>=1$ then $p_{i}≠p_{j}$ .
输出格式
Print the number of ways to fill the gaps modulo $10^{9}+7$ (i.e. modulo $1000000007$ ).
输入输出样例
输入 #1
4 0 0 0 0
输出 #1
4
输入 #2
5 0 0 1 2 0
输出 #2
2
输入 #3
6 0 0 1 2 0 0
输出 #3
0
输入 #4
5 5 3 4 2 1
输出 #4
0
In the first sample test, none of four element is fixed. There are four permutations satisfying the given conditions: (1,2,3,4), (1,4,3,2), (3,2,1,4), (3,4,1,2).
In the second sample test, there must be $p_{3}=1$ and $p_{4}=2$ . The two permutations satisfying the conditions are: (3,4,1,2,5), (5,4,1,2,3).
In the second sample test, there must be $p_{3}=1$ and $p_{4}=2$ . The two permutations satisfying the conditions are: (3,4,1,2,5), (5,4,1,2,3).
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted