CF2249B.Permutation Cuts

普及+/提高

通过率:0%

时间限制:4.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

You are given an integer nn and an array aa of length n−1n-1.

For a permutation∗^{\text{∗}} pp of length nn, define

v_i=minleft(max_1lejleip_j,max_i+1lejlenp_jright),v\_i=\\min\\left(\\max\_{1\\le j\\le i}p\_j, \\max\_{i+1\\le j\\le n}p\_j\\right),

where 1≤i≤n−11\le i\le n-1.

In other words, cut the permutation between positions ii and i+1i+1, take the maximum element on each side, and let viv_i be the smaller of these two values.

Your task is to count the number of permutations pp of length nn such that vi=aiv_i=a_i for each 1≤i≤n−11\le i\le n-1.

Output the answer 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).

给你一个整数 nn 和一个长度为 n−1n-1 的数组 aa。

对于一个长度为 nn 的排列∗^{\text{∗}} pp,定义

v_i=minleft(max_1lejleip_j,max_i+1lejlenp_jright),v\_i=\\min\\left(\\max\_{1\\le j\\le i}p\_j, \\max\_{i+1\\le j\\le n}p\_j\\right),

其中 1≤i≤n−11\le i\le n-1。

换言之,将排列在位置 ii 与 i+1i+1 之间切开,取左右两段各自的最大值,并令 viv_i 为这两个最大值中的较小者。

你的任务是计算满足对每个 1≤i≤n−11\le i\le n-1 均有 vi=aiv_i=a_i 的长度为 nn 的排列 pp 的个数。

输出答案对 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 one integer nn (2≤n≤1062\le n\le 10^6) — the length of pp.

The second line contains n−1n-1 integers a1,a2,…,an−1a_1,a_2,\ldots,a_{n-1} (1≤ai≤n1\le a_i\le n) — the elements of aa.

It is guaranteed that the sum of nn over all test cases does not exceed 10610^6.

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

每个测试用例的第一行包含一个整数 nn(2≤n≤1062\le n\le 10^6)——即 pp 的长度。

第二行包含 n−1n-1 个整数 a1,a2,…,an−1a_1,a_2,\ldots,a_{n-1}(1≤ai≤n1\le a_i\le n)——即数组 aa 的元素。

保证所有测试用例的 nn 之和不超过 10610^6。

输出格式

For each test case, output one integer — the number of suitable permutations, modulo 998 244 353998\,244\,353.

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

输入输出样例

  • 输入#1

    11
    2
    1
    3
    2 2
    3
    1 1
    4
    2 3 2
    5
    3 3 4 2
    2
    2
    3
    1 2
    4
    3 3 3
    5
    4 4 4 4
    4
    2 1 2
    6
    3 3 5 5 5

    输出#1

    2
    2
    0
    0
    2
    0
    2
    4
    12
    0
    8

说明/提示

In the first test case, both permutations [1,2][1,2] and [2,1][2,1] satisfy v1=1v_1=1.

In the second test case, the only suitable permutations are [2,1,3][2,1,3] and [3,1,2][3,1,2].

In the third test case, no suitable permutation exists.

在第一个测试用例中,排列 [1,2][1,2] 和 [2,1][2,1] 均满足 v1=1v_1=1。

在第二个测试用例中,唯一满足条件的排列是 [2,1,3][2,1,3] 和 [3,1,2][3,1,2]。

在第三个测试用例中,不存在满足条件的排列。

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

首页