CF2154F1.Bombing (Easy Version)

省选/NOI-

通过率:0%

时间限制:2.00s

内存限制:512MB

AC君温馨提醒

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

题目描述

This is the easy version of the problem. The difference between the versions is that in this version, n≤3000n \le 3000. You can hack only if you solved all versions of this problem.

A permutation∗^{\text{∗}} bb is considered a riffle shuffle of a permutation aa if ∣a∣=∣b∣|a| = |b| and there exists kk where 1≤k<∣a∣1 \le k \lt |a| such that a1,a2,…,aka_1,a_2,\ldots,a_k and ak+1,ak+2,…,a∣a∣a_{k + 1},a_{k + 2},\ldots,a_{|a|} are both subsequences†^{\text{†}} of bb.

For example, [1,4,5,2,3,6][\color{red}{1}, \color{blue}{4}, \color{blue}{5}, \color{red}{2}, \color{red}{3}, \color{blue}{6}] is a riffle shuffle of [1,2,3,4,5,6][\color{red}{1}, \color{red}{2}, \color{red}{3}, \color{blue}{4}, \color{blue}{5}, \color{blue}{6}] because we can select k=3k = 3 and both [1,2,3][\color{red}{1}, \color{red}{2}, \color{red}{3}] and [4,5,6][\color{blue}{4}, \color{blue}{5}, \color{blue}{6}] are subsequences.

You are given a permutation pp of length nn where some values are replaced with −1-1. Determine the number of ways to replace each −1-1 with an integer such that pp becomes a riffle shuffle of [1,2,…,n][1,2,\ldots,n] (the sorted permutation).

The number of ways could be gargantuan, so 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).

†^{\text{†}}A sequence cc is a subsequence of a sequence dd if cc can be obtained from dd by the deletion of several (possibly, zero or all) element from arbitrary positions.

这是该问题的简单版本。两个版本的区别在于,在此版本中,n≤3000n \le 3000。仅当您解决了该问题的所有版本后,才可进行 Hack。

若排列∗^{\text{∗}} bb 满足 ∣a∣=∣b∣|a| = |b|,且存在整数 kk(其中 1≤k<∣a∣1 \le k \lt |a|),使得 a1,a2,…,aka_1,a_2,\ldots,a_k 和 ak+1,ak+2,…,a∣a∣a_{k + 1},a_{k + 2},\ldots,a_{|a|} 均为 bb 的子序列†^{\text{†}},则称 bb 是排列 aa 的一次洗牌(riffle shuffle)。

例如,[1,4,5,2,3,6][\color{red}{1}, \color{blue}{4}, \color{blue}{5}, \color{red}{2}, \color{red}{3}, \color{blue}{6}] 是 [1,2,3,4,5,6][\color{red}{1}, \color{red}{2}, \color{red}{3}, \color{blue}{4}, \color{blue}{5}, \color{blue}{6}] 的一次洗牌,因为可取 k=3k = 3,此时 [1,2,3][\color{red}{1}, \color{red}{2}, \color{red}{3}] 和 [4,5,6][\color{blue}{4}, \color{blue}{5}, \color{blue}{6}] 均为前者的一个子序列。

给定一个长度为 nn 的排列 pp,其中部分元素被替换为 −1-1。请确定将每个 −1-1 替换为某个整数的方案数,使得 pp 成为 [1,2,…,n][1,2,\ldots,n](即升序排列)的一次洗牌。

答案可能非常巨大,请对 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)。

†^{\text{†}} 序列 cc 是序列 dd 的子序列,当且仅当 cc 可通过从 dd 中删除若干(可能为零个或全部)任意位置的元素而得到。

输入格式

Each test contains multiple test cases. The first line contains the number of test cases tt (1≤t≤1001 \le t \le 100). The description of the test cases follows.

The first line of each test case contains an integer nn (2≤n≤30002 \le n \le 3000) — the length of the permutation.

The second line of each test case contains nn integers p1,p2,…,pnp_1,p_2,\ldots,p_n (1≤pi≤n1 \le p_i \le n or pi=−1p_i = -1) — the elements of pp. All elements of pp that are not −1-1 are distinct.

The sum of nn across all test cases does not exceed 30003000.

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

每个测试用例的第一行包含一个整数 nn(2≤n≤30002 \le n \le 3000)—— 排列的长度。

每个测试用例的第二行包含 nn 个整数 p1,p2,…,pnp_1,p_2,\ldots,p_n(1≤pi≤n1 \le p_i \le n 或 pi=−1p_i = -1)—— 排列 pp 的元素。pp 中所有不为 −1-1 的元素互不相同。

所有测试用例的 nn 值之和不超过 30003000。

输出格式

For each testcase, output the number of ways to fill pp so that it is a riffle shuffle of [1,2,…,n][1,2,\ldots,n] modulo 998 244 353998\,244\,353.

对于每个测试用例,输出满足条件的排列 pp 的个数(即 pp 是 [1,2,…,n][1,2,\ldots,n] 的一次洗牌排列),结果对 998 244 353998\,244\,353 取模。

输入输出样例

  • 输入#1

    7
    5
    -1 -1 -1 -1 -1
    4
    1 2 3 4
    5
    -1 -1 -1 2 -1
    6
    -1 3 2 1 -1 -1
    18
    11 -1 2 -1 -1 -1 -1 6 -1 -1 14 8 9 15 -1 -1 -1 -1
    6
    -1 3 -1 4 -1 5
    3
    -1 2 1

    输出#1

    27
    1
    6
    0
    32
    0
    0

说明/提示

The possible permutations for the third test case are as follows:

  • [1,3,4,2,5][\color{red}1, \color{blue}3, \color{blue}4, \color{red}2, \color{blue}5],
  • [1,4,5,2,3][\color{red}1, \color{blue}4, \color{blue}5, \color{red}2, \color{red}3],
  • [3,1,4,2,5][\color{blue}3, \color{red}1, \color{blue}4, \color{red}2, \color{blue}5],
  • [3,4,1,2,5][\color{blue}3, \color{blue}4, \color{red}1, \color{red}2, \color{blue}5],
  • [4,1,5,2,3][\color{blue}4, \color{red}1, \color{blue}5, \color{red}2, \color{red}3],
  • [4,5,1,2,3][\color{blue}4, \color{blue}5, \color{red}1, \color{red}2, \color{red}3].

第三个测试用例的所有可能排列如下:

  • [1,3,4,2,5][\color{red}1, \color{blue}3, \color{blue}4, \color{red}2, \color{blue}5],
  • [1,4,5,2,3][\color{red}1, \color{blue}4, \color{blue}5, \color{red}2, \color{red}3],
  • [3,1,4,2,5][\color{blue}3, \color{red}1, \color{blue}4, \color{red}2, \color{blue}5],
  • [3,4,1,2,5][\color{blue}3, \color{blue}4, \color{red}1, \color{red}2, \color{blue}5],
  • [4,1,5,2,3][\color{blue}4, \color{red}1, \color{blue}5, \color{red}2, \color{red}3],
  • [4,5,1,2,3][\color{blue}4, \color{blue}5, \color{red}1, \color{red}2, \color{red}3].

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

首页