CF1698E.PermutationForces II

提高+/省选-

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

You are given a permutation aa of length nn. Recall that permutation is an array consisting of nn distinct integers from 11 to nn in arbitrary order.

You have a strength of ss and perform nn moves on the permutation aa. The ii-th move consists of the following:

  • Pick two integers xx and yy such that i≤x≤y≤min⁡(i+s,n)i \leq x \leq y \leq \min(i+s,n), and swap the positions of the integers xx and yy in the permutation aa. Note that you can select x=yx=y in the operation, in which case no swap will occur.

You want to turn aa into another permutation bb after nn moves. However, some elements of bb are missing and are replaced with −1-1 instead. Count the number of ways to replace each −1-1 in bb with some integer from 11 to nn so that bb is a permutation and it is possible to turn aa into bb with a strength of ss.

Since the answer can be large, output it modulo 998 244 353998\,244\,353.

你有一个长度为 nn 的排列 aa。回忆一下,排列是由 11 到 nn 的 nn 个互不相同的整数组成的、顺序任意的数组。

你拥有强度 ss,并在排列 aa 上执行 nn 次操作。第 ii 次操作包含以下步骤:

  • 选择两个整数 xx 和 yy,满足 i≤x≤y≤min⁡(i+s,n)i \leq x \leq y \leq \min(i+s,n),然后在排列 aa 中交换整数 xx 和 yy 的位置。注意,你可以在该操作中选择 x=yx = y,此时不发生任何交换。

你希望经过 nn 次操作后将 aa 变为另一个排列 bb。然而,bb 中某些元素缺失,被替换为 −1-1。请计算将 bb 中每个 −1-1 替换为 11 到 nn 中某个整数的方式数目,使得 bb 成为一个合法排列,并且存在一种方式,在强度为 ss 的条件下,能将 aa 变为该 bb。

由于答案可能很大,请对 998 244 353998\,244\,353 取模输出结果。

输入格式

The input consists of multiple test cases. The first line contains an integer tt (1≤t≤10001 \leq t \leq 1000) — the number of test cases. The description of the test cases follows.

The first line of each test case contains two integers nn and ss (1≤n≤2⋅1051 \leq n \leq 2 \cdot 10^5; 1≤s≤n1 \leq s \leq n) — the size of the permutation and your strength, respectively.

The second line of each test case contains nn integers a1,a2,…,ana_1, a_2, \ldots, a_n (1≤ai≤n1 \le a_i \le n) — the elements of aa. All elements of aa are distinct.

The third line of each test case contains nn integers b1,b2,…,bnb_1, b_2, \ldots, b_n (1≤bi≤n1 \le b_i \le n or bi=−1b_i = -1) — the elements of bb. All elements of bb that are not equal to −1-1 are distinct.

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

输入包含多个测试用例。第一行包含一个整数 tt(1≤t≤10001 \leq t \leq 1000),表示测试用例的数量。随后是各测试用例的描述。

每个测试用例的第一行包含两个整数 nn 和 ss(1≤n≤2⋅1051 \leq n \leq 2 \cdot 10^5;1≤s≤n1 \leq s \leq n),分别表示排列的大小和你的力量值。

每个测试用例的第二行包含 nn 个整数 a1,a2,…,ana_1, a_2, \ldots, a_n(1≤ai≤n1 \le a_i \le n),即数组 aa 的元素。aa 的所有元素互不相同。

每个测试用例的第三行包含 nn 个整数 b1,b2,…,bnb_1, b_2, \ldots, b_n(1≤bi≤n1 \le b_i \le n 或 bi=−1b_i = -1),即数组 bb 的元素。bb 中所有不等于 −1-1 的元素互不相同。

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

输出格式

For each test case, output a single integer — the number of ways to fill up the permutation bb so that it is possible to turn aa into bb using a strength of ss, modulo 998 244 353998\,244\,353.

对于每个测试用例,输出一个整数——即填满排列 bb 的方案数,使得能够使用强度 ss 将 aa 变为 bb,结果对 998 244 353998\,244\,353 取模。

输入输出样例

  • 输入#1

    6
    3 1
    2 1 3
    3 -1 -1
    3 2
    2 1 3
    3 -1 -1
    4 1
    1 4 3 2
    4 3 1 2
    6 4
    4 2 6 3 1 5
    6 1 5 -1 3 -1
    7 4
    1 3 6 2 7 4 5
    2 5 -1 -1 -1 4 -1
    14 14
    1 2 3 4 5 6 7 8 9 10 11 12 13 14
    -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1

    输出#1

    1
    2
    0
    2
    12
    331032489

说明/提示

In the first test case, a=[2,1,3]a=[2,1,3]. There are two possible ways to fill out the −1-1s in bb to make it a permutation: [3,1,2][3,1,2] or [3,2,1][3,2,1]. We can make aa into [3,1,2][3,1,2] with a strength of 11 as follows: $$[2,1,3] \xrightarrow[x=1,\,y=1]{} [2,1,3] \xrightarrow[x=2,\,y=3]{} [3,1,2] \xrightarrow[x=3,\,y=3]{} [3,1,2].$$ It can be proven that it is impossible to make [2,1,3][2,1,3] into [3,2,1][3,2,1] with a strength of 11. Thus only one permutation bb satisfies the constraints, so the answer is 11.

In the second test case, aa and bb the same as the previous test case, but we now have a strength of 22. We can make aa into [3,2,1][3,2,1] with a strength of 22 as follows: $$[2,1,3] \xrightarrow[x=1,\,y=3]{} [2,3,1] \xrightarrow[x=2,\,y=3]{} [3,2,1] \xrightarrow[x=3,\,y=3]{} [3,2,1].$$ We can still make aa into [3,1,2][3,1,2] using a strength of 11 as shown in the previous test case, so the answer is 22.

In the third test case, there is only one permutation bb. It can be shown that it is impossible to turn aa into bb, so the answer is 00.

在第一个测试用例中,a=[2,1,3]a=[2,1,3]。将 bb 中的 −1-1 填充为一个排列共有两种可能:[3,1,2][3,1,2] 或 [3,2,1][3,2,1]。我们可以用强度 11 将 aa 变为 [3,1,2][3,1,2],过程如下:

\[2,1,3\] \\xrightarrow\[x=1,\\,y=1\]{} \[2,1,3\] \\xrightarrow\[x=2,\\,y=3\]{} \[3,1,2\] \\xrightarrow\[x=3,\\,y=3\]{} \[3,1,2\].

可以证明:无法用强度 11 将 [2,1,3][2,1,3] 变为 [3,2,1][3,2,1]。因此,仅有一个排列 bb 满足约束条件,答案为 11。

在第二个测试用例中,aa 和 bb 与上一测试用例相同,但现在强度为 22。我们可以用强度 22 将 aa 变为 [3,2,1][3,2,1],过程如下:

\[2,1,3\] \\xrightarrow\[x=1,\\,y=3\]{} \[2,3,1\] \\xrightarrow\[x=2,\\,y=3\]{} \[3,2,1\] \\xrightarrow\[x=3,\\,y=3\]{} \[3,2,1\].

如前一测试用例所示,我们仍可用强度 11 将 aa 变为 [3,1,2][3,1,2],因此答案为 22。

在第三个测试用例中,仅存在一个排列 bb。可以证明:无法将 aa 变为 bb,因此答案为 00。

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

首页