CF2190E.Median Permutation

NOI/NOI+/CTSC

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

For a permutation∗^{\text{∗}} qq of size m≥3m \ge 3, define f(q)f(q) to be a sequence bb of size m−2m - 2 such that bi=med⁡(qi,qi+1,qi+2)b_i = \operatorname{med}(q_i, q_{i + 1}, q_{i + 2}) for all 1≤i≤m−21 \le i \le m - 2. Here, med⁡(x,y,z)\operatorname{med}(x, y, z) denotes the second smallest element among x,y,z{x, y, z}.

You are given an array aa of size nn, where some elements may be 00. It is guaranteed that aa contains the values 11 and nn (that is, there exist indices i,ji, j such that ai=1a_i = 1 and aj=na_j = n).

Find the number of permutations pp of size nn satisfying the following conditions:

  • pp is consistent with aa: for all 1≤i≤n1 \le i \le n, if ai≠0a_i \neq 0, then pi=aip_i = a_i.
  • All elements of f(p)f(p) are distinct.

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

∗^{\text{∗}}A permutation of length nn 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).

对于一个长度为 m≥3m \ge 3 的排列∗^{\text{∗}} qq,定义 f(q)f(q) 为一个长度为 m−2m - 2 的序列 bb,其中对所有 1≤i≤m−21 \le i \le m - 2,有 bi=med⁡(qi,qi+1,qi+2)b_i = \operatorname{med}(q_i, q_{i + 1}, q_{i + 2})。此处,med⁡(x,y,z)\operatorname{med}(x, y, z) 表示集合 {x,y,z}\{x, y, z\} 中第二小的元素。

给定一个长度为 nn 的数组 aa,其中某些元素可能为 00。保证 aa 中包含数值 11 和 nn(即存在下标 i,ji, j,使得 ai=1a_i = 1 且 aj=na_j = n)。

请找出满足以下条件的长度为 nn 的排列 pp 的个数:

  • pp 与 aa 一致:对所有 1≤i≤n1 \le i \le n,若 ai≠0a_i \neq 0,则 pi=aip_i = a_i;
  • f(p)f(p) 中的所有元素互不相同。

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

∗^{\text{∗}} 长度为 nn 的排列是指由 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)。

输入格式

Each test contains multiple test cases. The first line contains the number of test cases tt (1≤t≤1041 \le t \le 10^4). The description of the test cases follows.

The first line of each test case contains a single integer nn (3≤n≤2⋅1053 \le n \le 2 \cdot 10^5) — the size of the array.

The second line contains nn integers a1,a2,…,ana_1, a_2, \ldots, a_n (0≤ai≤n0 \le a_i \le n).

It is guaranteed that all non-zero elements of aa are pairwise distinct. It is also guaranteed that aa contains the values 11 and nn.

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

每个测试包含多个测试用例。第一行包含测试用例的数量 tt(1≤t≤1041 \le t \le 10^4)。随后是各测试用例的描述。

每个测试用例的第一行包含一个整数 nn(3≤n≤2⋅1053 \le n \le 2 \cdot 10^5)—— 数组的大小。

第二行包含 nn 个整数 a1,a2,…,ana_1, a_2, \ldots, a_n(0≤ai≤n0 \le a_i \le n)。

保证数组 aa 中所有非零元素两两不同。同时保证 aa 中包含数值 11 和 nn。

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

输出格式

For each test case, print a single integer — the number of permutations pp satisfying the conditions, modulo 998 244 353998\,244\,353.

对于每个测试用例,输出一个整数——满足条件的排列 pp 的数量,对 998 244 353998\,244\,353 取模。

输入输出样例

  • 输入#1

    5
    3
    1 3 2
    5
    0 5 4 1 0
    7
    0 0 1 0 0 7 0
    10
    1 10 0 0 0 0 0 0 0 0
    15
    0 0 10 0 0 15 0 0 6 7 0 1 0 0 3

    输出#1

    1
    0
    10
    1
    4

说明/提示

In the first example, the only permutation consistent with the input is p=[1,3,2]p = [1, 3, 2]. We have f(p)=[2]f(p) = [2], since med⁡(1,3,2)=2\operatorname{med}(1, 3, 2) = 2. The elements of f(p)f(p) are distinct, so this permutation is valid. The answer is 11.

In the second example, there are two permutations consistent with the input: p=[3,5,4,1,2]p = [3, 5, 4, 1, 2] and p=[2,5,4,1,3]p = [2, 5, 4, 1, 3].

  • For p=[3,5,4,1,2]p = [3, 5, 4, 1, 2], f(p)=[4,4,2]f(p) = [4, 4, 2].
  • For p=[2,5,4,1,3]p = [2, 5, 4, 1, 3], f(p)=[4,4,3]f(p) = [4, 4, 3].

In both cases, the value 44 appears twice in f(p)f(p). Thus, there are no valid permutations, and the answer is 00.

在第一个例子中,唯一与输入一致的排列是 p=[1,3,2]p = [1, 3, 2]。我们有 f(p)=[2]f(p) = [2],因为 med⁡(1,3,2)=2\operatorname{med}(1, 3, 2) = 2。f(p)f(p) 的元素互不相同,因此该排列是合法的。答案为 11。

在第二个例子中,有两个与输入一致的排列:p=[3,5,4,1,2]p = [3, 5, 4, 1, 2] 和 p=[2,5,4,1,3]p = [2, 5, 4, 1, 3]。

  • 对于 p=[3,5,4,1,2]p = [3, 5, 4, 1, 2],有 f(p)=[4,4,2]f(p) = [4, 4, 2]。
  • 对于 p=[2,5,4,1,3]p = [2, 5, 4, 1, 3],有 f(p)=[4,4,3]f(p) = [4, 4, 3]。

在这两种情况下,值 44 在 f(p)f(p) 中均出现了两次。因此,不存在合法的排列,答案为 00。

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

首页