CF1677D.Tokitsukaze and Permutations

省选/NOI-

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

Tokitsukaze has a permutation pp. She performed the following operation to pp exactly kk times: in one operation, for each ii from 11 to n−1n - 1 in order, if pip_i > pi+1p_{i+1}, swap pip_i, pi+1p_{i+1}. After exactly kk times of operations, Tokitsukaze got a new sequence aa, obviously the sequence aa is also a permutation.

After that, Tokitsukaze wrote down the value sequence vv of aa on paper. Denote the value sequence vv of the permutation aa of length nn as vi=∑j=1i−1[ai<aj]v_i=\sum_{j=1}^{i-1}[a_i \lt a_j], where the value of [ai<aj][a_i \lt a_j] define as if ai<aja_i \lt a_j, the value is 11, otherwise is 00 (in other words, viv_i is equal to the number of elements greater than aia_i that are to the left of position ii). Then Tokitsukaze went out to work.

There are three naughty cats in Tokitsukaze's house. When she came home, she found the paper with the value sequence vv to be bitten out by the cats, leaving several holes, so that the value of some positions could not be seen clearly. She forgot what the original permutation pp was. She wants to know how many different permutations pp there are, so that the value sequence vv of the new permutation aa after exactly kk operations is the same as the vv written on the paper (not taking into account the unclear positions).

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

Tokitsukaze 有一个排列 pp。她对 pp 恰好执行了 kk 次如下操作:每次操作中,按 ii 从 11 到 n−1n - 1 的顺序,对每个 ii,若 pi>pi+1p_i > p_{i+1},则交换 pip_i 与 pi+1p_{i+1}。经过恰好 kk 次操作后,Tokitsukaze 得到了一个新的序列 aa;显然,序列 aa 也是一个排列。

之后,Tokitsukaze 将排列 aa 的“值序列”vv 记录在纸上。长度为 nn 的排列 aa 的值序列 vv 定义为:

vi=∑j=1i−1[ai<aj],v_i = \sum_{j=1}^{i-1}[a_i < a_j],

其中 [ai<aj][a_i < a_j] 是一个指示函数:当 ai<aja_i < a_j 时值为 11,否则为 00(换言之,viv_i 等于位于位置 ii 左侧且大于 aia_i 的元素个数)。随后,Tokitsukaze 出门工作。

Tokitsukaze 家中有三只淘气的猫。她回家后发现记录值序列 vv 的纸被猫咬出了若干个洞,导致某些位置上的数值变得模糊不清。她已记不得原始排列 pp 是什么了。她想知道:有多少个不同的排列 pp,使得在恰好执行 kk 次上述操作后得到的新排列 aa 所对应的值序列 vv,与纸上所记录的 vv(忽略那些模糊不清的位置)完全一致?

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

输入格式

The first line contains a single integer tt (1≤t≤10001 \leq t \leq 1000) — the number of test cases. Each test case consists of two lines.

The first line contains two integers nn and kk (1≤n≤1061 \leq n \leq 10^6; 0≤k≤n−10 \leq k \leq n-1) — the length of the permutation and the exactly number of operations.

The second line contains nn integers v1,v2,…,vnv_1, v_2, \dots, v_n (−1≤vi≤i−1-1 \leq v_i \leq i-1) — the value sequence vv. vi=−1v_i = -1 means the ii-th position of vv can't be seen clearly.

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

第一行包含一个整数 tt(1≤t≤10001 \leq t \leq 1000),表示测试用例的数量。每个测试用例由两行组成。

第一行包含两个整数 nn 和 kk(1≤n≤1061 \leq n \leq 10^6;0≤k≤n−10 \leq k \leq n-1),分别表示排列的长度以及恰好执行的操作次数。

第二行包含 nn 个整数 v1,v2,…,vnv_1, v_2, \dots, v_n(−1≤vi≤i−1-1 \leq v_i \leq i-1),即值序列 vv。其中 vi=−1v_i = -1 表示 vv 的第 ii 个位置模糊不清,无法辨认。

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

输出格式

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

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

输入输出样例

  • 输入#1

    3
    5 0
    0 1 2 3 4
    5 2
    -1 1 2 0 0
    5 2
    0 1 1 0 0

    输出#1

    1
    6
    6

说明/提示

In the first test case, only permutation p=[5,4,3,2,1]p=[5,4,3,2,1] satisfies the constraint condition.

In the second test case, there are 66 permutations satisfying the constraint condition, which are:

  • [3,4,5,2,1][3,4,5,2,1] →\rightarrow [3,4,2,1,5][3,4,2,1,5] →\rightarrow [3,2,1,4,5][3,2,1,4,5]
  • [3,5,4,2,1][3,5,4,2,1] →\rightarrow [3,4,2,1,5][3,4,2,1,5] →\rightarrow [3,2,1,4,5][3,2,1,4,5]
  • [4,3,5,2,1][4,3,5,2,1] →\rightarrow [3,4,2,1,5][3,4,2,1,5] →\rightarrow [3,2,1,4,5][3,2,1,4,5]
  • [4,5,3,2,1][4,5,3,2,1] →\rightarrow [4,3,2,1,5][4,3,2,1,5] →\rightarrow [3,2,1,4,5][3,2,1,4,5]
  • [5,3,4,2,1][5,3,4,2,1] →\rightarrow [3,4,2,1,5][3,4,2,1,5] →\rightarrow [3,2,1,4,5][3,2,1,4,5]
  • [5,4,3,2,1][5,4,3,2,1] →\rightarrow [4,3,2,1,5][4,3,2,1,5] →\rightarrow [3,2,1,4,5][3,2,1,4,5]

So after exactly 22 times of swap they will all become a=[3,2,1,4,5]a=[3,2,1,4,5], whose value sequence is v=[0,1,2,0,0]v=[0,1,2,0,0].

在第一个测试用例中,仅有排列 p=[5,4,3,2,1]p=[5,4,3,2,1] 满足约束条件。

在第二个测试用例中,共有 66 个满足约束条件的排列,它们是:

  • [3,4,5,2,1][3,4,5,2,1] →\rightarrow [3,4,2,1,5][3,4,2,1,5] →\rightarrow [3,2,1,4,5][3,2,1,4,5]
  • [3,5,4,2,1][3,5,4,2,1] →\rightarrow [3,4,2,1,5][3,4,2,1,5] →\rightarrow [3,2,1,4,5][3,2,1,4,5]
  • [4,3,5,2,1][4,3,5,2,1] →\rightarrow [3,4,2,1,5][3,4,2,1,5] →\rightarrow [3,2,1,4,5][3,2,1,4,5]
  • [4,5,3,2,1][4,5,3,2,1] →\rightarrow [4,3,2,1,5][4,3,2,1,5] →\rightarrow [3,2,1,4,5][3,2,1,4,5]
  • [5,3,4,2,1][5,3,4,2,1] →\rightarrow [3,4,2,1,5][3,4,2,1,5] →\rightarrow [3,2,1,4,5][3,2,1,4,5]
  • [5,4,3,2,1][5,4,3,2,1] →\rightarrow [4,3,2,1,5][4,3,2,1,5] →\rightarrow [3,2,1,4,5][3,2,1,4,5]

因此,经过恰好 22 次交换后,它们均变为 a=[3,2,1,4,5]a=[3,2,1,4,5],其值序列为 v=[0,1,2,0,0]v=[0,1,2,0,0]。

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

首页