CF698F.Coprime Permutation

NOI/NOI+/CTSC

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

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 109 + 7.

两个正整数互质,当且仅当它们没有大于 1 的公共因数。

某只熊不愿告诉 Radewoosh 如何解决某个算法问题。因此,Radewoosh 打算潜入这只熊存放解法的保险箱。要通过门前的通道,他必须输入一个 11 到 nn 的排列。当且仅当所输入的排列 p1, p2, ..., pnp_1,\,p_2,\,...,\,p_n 满足以下条件时,门才会打开:

换言之,排列中两个不同元素互质,当且仅当它们的下标互质。

该排列中部分位置的元素可能已被预先固定。Radewoosh 有多少种方式填满剩余空位,使得门能够打开?请输出答案对 109+710^9 + 7 取模的结果。

输入格式

The first line of the input contains one integer n (2 ≤ n ≤ 1 000 000).

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.

输入的第一行包含一个整数 nn(2≤n≤1 000 0002 \leq n \leq 1\,000\,000)。

第二行包含 nn 个整数 p1, p2, …, pnp_1,\,p_2,\,\dots,\,p_n(0≤pi≤n0 \leq p_i \leq n),其中 pi=0p_i = 0 表示待填充的空位,而 pi≥1p_i \geq 1 表示一个固定的数字。

保证:若 i≠ji \neq j 且 pi, pj≥1p_i,\,p_j \geq 1,则 pi≠pjp_i \neq p_j。

输出格式

Print the number of ways to fill the gaps modulo 109 + 7 (i.e. modulo 1000000007).

输出填满空隙的方案数对 109+710^9 + 7(即对 10000000071000000007 取模)的结果。

输入输出样例

  • 输入#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).

在第一个样例测试中,四个元素均未被固定。共有四种满足给定条件的排列:(1,2,3,4)(1,2,3,4)、(1,4,3,2)(1,4,3,2)、(3,2,1,4)(3,2,1,4)、(3,4,1,2)(3,4,1,2)。

在第二个样例测试中,必须有 p3 = 1p_3 = 1 且 p4 = 2p_4 = 2。满足条件的两种排列为:(3,4,1,2,5)(3,4,1,2,5)、(5,4,1,2,3)(5,4,1,2,3)。

输入解题思路,AI测评打分。不知道怎么写?

首页