CF1670C.Where is the Pizza?

普及/提高-

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

While searching for the pizza, baby Hosssam came across two permutations aa and bb of length nn.

Recall that a permutation is an array consisting of nn distinct integers from 11 to nn in arbitrary order. For example, [2,3,1,5,4][2,3,1,5,4] is a permutation, but [1,2,2][1,2,2] is not a permutation (22 appears twice in the array) and [1,3,4][1,3,4] is also not a permutation (n=3n=3 but there is 44 in the array).

Baby Hosssam forgot about the pizza and started playing around with the two permutations. While he was playing with them, some elements of the first permutation got mixed up with some elements of the second permutation, and to his surprise those elements also formed a permutation of size nn.

Specifically, he mixed up the permutations to form a new array cc in the following way.

  • For each ii (1≤i≤n1\le i\le n), he either made ci=aic_i=a_i or ci=bic_i=b_i.
  • The array cc is a permutation.

You know permutations aa, bb, and values at some positions in cc. Please count the number different permutations cc that are consistent with the described process and the given values. Since the answer can be large, print it modulo 109+710^9+7.

It is guaranteed that there exists at least one permutation cc that satisfies all the requirements.

在寻找披萨的过程中,小 Hosssam 偶然发现了两个长度为 nn 的排列 aa 和 bb。

回忆一下,一个排列是由 11 到 nn 中互不相同的 nn 个整数以任意顺序组成的数组。例如,[2,3,1,5,4][2,3,1,5,4] 是一个排列,但 [1,2,2][1,2,2] 不是排列(数组中数字 22 出现了两次),[1,3,4][1,3,4] 也不是排列(此时 n=3n=3,但数组中出现了 44)。

小 Hosssam 忘记了披萨,开始摆弄这两个排列。在他摆弄的过程中,第一个排列中的一些元素与第二个排列中的一些元素混在了一起,令他惊讶的是,这些混在一起的元素也构成了一个长度为 nn 的排列。

具体来说,他按如下方式将两个排列混合,构造出一个新数组 cc:

  • 对每个 ii(1≤i≤n1 \le i \le n),他要么令 ci=aic_i = a_i,要么令 ci=bic_i = b_i;
  • 数组 cc 是一个排列。

你已知排列 aa、bb,以及数组 cc 中某些位置上的值。请计算满足上述构造过程及给定值的不同排列 cc 的数量。由于答案可能很大,请对 109+710^9+7 取模输出。

保证至少存在一个满足所有要求的排列 cc。

输入格式

The first line contains an integer tt (1≤t≤1051 \le t \le 10^5) — the number of test cases.

The first line of each test case contains a single integer nn (1≤n≤1051\le n\le 10^5) — the length of the permutations.

The next line contains nn distinct integers a1,a2,…,ana_1,a_2,\ldots,a_n (1≤ai≤n1\le a_i\le n) — the first permutation.

The next line contains nn distinct integers b1,b2,…,bnb_1,b_2,\ldots,b_n (1≤bi≤n1\le b_i\le n) — the second permutation.

The next line contains nn distinct integers d1,d2,…,dnd_1,d_2,\ldots,d_n (did_i is either 00, aia_i, or bib_i) — the description of the known values of cc. If di=0d_i=0, then there are no requirements on the value of cic_i. Otherwise, it is required that ci=dic_i=d_i.

It is guaranteed that there exists at least one permutation cc that satisfies all the requirements.

It is guaranteed that the sum of nn over all test cases does not exceed 5⋅1055 \cdot 10^5.

第一行包含一个整数 tt(1≤t≤1051 \le t \le 10^5)—— 表示测试用例的数量。

每个测试用例的第一行包含一个整数 nn(1≤n≤1051\le n\le 10^5)—— 表示排列的长度。

接下来一行包含 nn 个互不相同的整数 a1,a2,…,ana_1,a_2,\ldots,a_n(1≤ai≤n1\le a_i\le n)—— 第一个排列。

接下来一行包含 nn 个互不相同的整数 b1,b2,…,bnb_1,b_2,\ldots,b_n(1≤bi≤n1\le b_i\le n)—— 第二个排列。

接下来一行包含 nn 个互不相同的整数 d1,d2,…,dnd_1,d_2,\ldots,d_n(每个 did_i 的值为 00、aia_i 或 bib_i)—— 描述已知的 cc 的值。若 di=0d_i=0,则对 cic_i 的值无任何限制;否则要求 ci=dic_i=d_i。

保证至少存在一个满足所有约束条件的排列 cc。

保证所有测试用例的 nn 之和不超过 5⋅1055 \cdot 10^5。

输出格式

For each test case, print the number of possible permutations cc, modulo 109+710^9+7.

对于每个测试用例,输出可能的排列数 cc 对 109+710^9+7 取模的结果。

输入输出样例

  • 输入#1

    9
    7
    1 2 3 4 5 6 7
    2 3 1 7 6 5 4
    2 0 1 0 0 0 0
    1
    1
    1
    0
    6
    1 5 2 4 6 3
    6 5 3 1 4 2
    6 0 0 0 0 0
    8
    1 6 4 7 2 3 8 5
    3 2 8 1 4 5 6 7
    1 0 0 7 0 3 0 5
    10
    1 8 6 2 4 7 9 3 10 5
    1 9 2 3 4 10 8 6 7 5
    1 9 2 3 4 10 8 6 7 5
    7
    1 2 3 4 5 6 7
    2 3 1 7 6 5 4
    0 0 0 0 0 0 0
    5
    1 2 3 4 5
    1 2 3 4 5
    0 0 0 0 0
    5
    1 2 3 4 5
    1 2 3 5 4
    0 0 0 0 0
    3
    1 2 3
    3 1 2
    0 0 0

    输出#1

    4
    1
    2
    2
    1
    8
    1
    2
    2

说明/提示

In the first test case, there are 44 distinct permutation that can be made using the process: [2,3,1,4,5,6,7][2,3,1,4,5,6,7], [2,3,1,7,6,5,4][2,3,1,7,6,5,4], [2,3,1,4,6,5,7][2,3,1,4,6,5,7], [2,3,1,7,5,6,4][2,3,1,7,5,6,4].

In the second test case, there is only one distinct permutation that can be made using the process: [1][1].

In the third test case, there are 22 distinct permutation that can be made using the process: [6,5,2,1,4,3][6,5,2,1,4,3], [6,5,3,1,4,2][6,5,3,1,4,2].

In the fourth test case, there are 22 distinct permutation that can be made using the process: [1,2,8,7,4,3,6,5][1,2,8,7,4,3,6,5], [1,6,4,7,2,3,8,5][1,6,4,7,2,3,8,5].

In the fifth test case, there is only one distinct permutation that can be made using the process: [1,9,2,3,4,10,8,6,7,5][1,9,2,3,4,10,8,6,7,5].

在第一个测试用例中,使用该过程可以生成 44 个不同的排列:[2,3,1,4,5,6,7][2,3,1,4,5,6,7]、[2,3,1,7,6,5,4][2,3,1,7,6,5,4]、[2,3,1,4,6,5,7][2,3,1,4,6,5,7]、[2,3,1,7,5,6,4][2,3,1,7,5,6,4]。

在第二个测试用例中,使用该过程只能生成 11 个不同的排列:[1][1]。

在第三个测试用例中,使用该过程可以生成 22 个不同的排列:[6,5,2,1,4,3][6,5,2,1,4,3]、[6,5,3,1,4,2][6,5,3,1,4,2]。

在第四个测试用例中,使用该过程可以生成 22 个不同的排列:[1,2,8,7,4,3,6,5][1,2,8,7,4,3,6,5]、[1,6,4,7,2,3,8,5][1,6,4,7,2,3,8,5]。

在第五个测试用例中,使用该过程只能生成 11 个不同的排列:[1,9,2,3,4,10,8,6,7,5][1,9,2,3,4,10,8,6,7,5]。

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

首页