CF2245F.Familiar?

省选/NOI-

通过率:0%

时间限制:5.00s

内存限制:128MB

AC君温馨提醒

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

题目描述

Note the unusual memory limit.

Consider the following pseudocode that processes a permutation pp of length nn∗^{\text{∗}}:

function mystery(p):    n = length of p    st = an empty stack    count = an array of length n filled with 0s    for i from 1 to n:        while st is not empty and p[i] < top of st:            pop from st            count[i] = count[i] + 1        push p[i] into st    return count

You are given an array aa of length nn. Your task is to count the number of permutations pp of length nn such that, if bb is the array returned by mystery(p), the condition ai=bia_i = b_i holds for all ii (1≤i≤n1 \le i \le n) where ai≠−1a_i \neq -1.

Since the answer can be very large, output 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).

注意特殊的内存限制。

考虑以下用于处理长度为 nn 的排列 pp 的伪代码∗^{\text{∗}}:

function mystery(p):    n = length of p    st = an empty stack    count = an array of length n filled with 0s    for i from 1 to n:        while st is not empty and p[i] < top of st:            pop from st            count[i] = count[i] + 1        push p[i] into st    return count

你被给定一个长度为 nn 的数组 aa。你的任务是统计满足如下条件的长度为 nn 的排列 pp 的个数:设 bb 是调用 mystery(p) 所返回的数组,对所有满足 ai≠−1a_i \neq -1 的下标 ii(1≤i≤n1 \le i \le n),均有 ai=bia_i = b_i。

由于答案可能非常大,请将结果对 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 an integer nn (1≤n≤5001 \le n \le 500), representing the length of aa.

The second line contains nn integers a1,a2,…,ana_1,a_2,\ldots,a_n (−1≤ai≤n-1 \le a_i \le n), representing the elements in aa.

It is guaranteed that the sum of n3n^3 over all test cases does not exceed 5003500^3.

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

每个测试用例的第一行包含一个整数 nn(1≤n≤5001 \le n \le 500),表示数组 aa 的长度。

第二行包含 nn 个整数 a1,a2,…,ana_1,a_2,\ldots,a_n(−1≤ai≤n-1 \le a_i \le n),表示数组 aa 中的元素。

保证所有测试用例中 n3n^3 的总和不超过 5003500^3。

输出格式

For each test case, output an integer representing the number of permutations, modulo 998 244 353998\,244\,353.

对于每个测试用例,输出一个整数,表示排列的数量对 998 244 353998\,244\,353 取模的结果。

输入输出样例

  • 输入#1

    7
    1
    0
    1
    -1
    1
    1
    4
    0 0 -1 1
    6
    -1 -1 -1 -1 -1 -1
    3
    0 1 0
    7
    0 -1 0 -1 2 1 -1

    输出#1

    1
    1
    0
    3
    720
    2
    77

说明/提示

In the first and second test cases, the only valid permutation is [1][1].

In the fourth test case, the valid permutations are [1,2,4,3][1,2,4,3], [3,4,2,1][3,4,2,1], and [1,4,3,2][1,4,3,2].

In the fifth test case, all permutations of length 66 are valid.

在第一和第二个测试用例中,唯一有效的排列是 [1][1]。

在第四个测试用例中,有效的排列有 [1,2,4,3][1,2,4,3]、[3,4,2,1][3,4,2,1] 和 [1,4,3,2][1,4,3,2]。

在第五个测试用例中,所有长度为 66 的排列均有效。

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

首页