CF2245F.Familiar?
省选/NOI-
通过率:0%
时间限制:5.00s
内存限制:128MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
Note the unusual memory limit.
Consider the following pseudocode that processes a permutation p of length n∗:
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 a of length n. Your task is to count the number of permutations p of length n such that, if b is the array returned by mystery(p), the condition ai=bi holds for all i (1≤i≤n) where ai=−1.
Since the answer can be very large, output it modulo 998244353.
∗A permutation of length n is an array consisting of n distinct integers from 1 to n in arbitrary order. For example, [2,3,1,5,4] is a permutation, but [1,2,2] is not a permutation (2 appears twice in the array), and [1,3,4] is also not a permutation (n=3 but there is 4 in the array).
注意特殊的内存限制。
考虑以下用于处理长度为 n 的排列 p 的伪代码∗:
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
你被给定一个长度为 n 的数组 a。你的任务是统计满足如下条件的长度为 n 的排列 p 的个数:设 b 是调用 mystery(p) 所返回的数组,对所有满足 ai=−1 的下标 i(1≤i≤n),均有 ai=bi。
由于答案可能非常大,请将结果对 998244353 取模后输出。
∗ 长度为 n 的排列是指由 1 到 n 中 n 个互不相同的整数以任意顺序组成的数组。例如,[2,3,1,5,4] 是一个排列,而 [1,2,2] 不是排列(数字 2 在数组中出现了两次),[1,3,4] 也不是排列(此时 n=3,但数组中出现了 4)。
输入格式
Each test contains multiple test cases. The first line contains the number of test cases t (1≤t≤104). The description of the test cases follows.
The first line of each test case contains an integer n (1≤n≤500), representing the length of a.
The second line contains n integers a1,a2,…,an (−1≤ai≤n), representing the elements in a.
It is guaranteed that the sum of n3 over all test cases does not exceed 5003.
每个测试包含多个测试用例。第一行包含测试用例的数量 t(1≤t≤104)。随后是各测试用例的描述。
每个测试用例的第一行包含一个整数 n(1≤n≤500),表示数组 a 的长度。
第二行包含 n 个整数 a1,a2,…,an(−1≤ai≤n),表示数组 a 中的元素。
保证所有测试用例中 n3 的总和不超过 5003。
输出格式
For each test case, output an integer representing the number of permutations, modulo 998244353.
对于每个测试用例,输出一个整数,表示排列的数量对 998244353 取模的结果。
输入输出样例
输入#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].
In the fourth test case, the valid permutations are [1,2,4,3], [3,4,2,1], and [1,4,3,2].
In the fifth test case, all permutations of length 6 are valid.
在第一和第二个测试用例中,唯一有效的排列是 [1]。
在第四个测试用例中,有效的排列有 [1,2,4,3]、[3,4,2,1] 和 [1,4,3,2]。
在第五个测试用例中,所有长度为 6 的排列均有效。
输入解题思路,AI测评打分。不知道怎么写?