CF1919E.Counting Prefixes

省选/NOI-

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

There is a hidden array aa of size nn consisting of only 11 and −1-1. Let pp be the prefix sums of array aa. More formally, pp is an array of length nn defined as pi=a1+a2+…+aip_i = a_1 + a_2 + \ldots + a_i. Afterwards, array pp is sorted in non-decreasing order. For example, if a=[1,−1,−1,1,1]a = [1, -1, -1, 1, 1], then p=[1,0,−1,0,1]p = [1, 0, -1, 0, 1] before sorting and p=[−1,0,0,1,1]p = [-1, 0, 0, 1, 1] after sorting.

You are given the prefix sum array pp after sorting, but you do not know what array aa is. Your task is to count the number of initial arrays aa such that the above process results in the given sorted prefix sum array pp. As this number can be large, you are only required to find it modulo 998 244 353998\,244\,353.

存在一个长度为 nn 的隐藏数组 aa,其中仅包含 11 和 −1-1。令 pp 表示数组 aa 的前缀和数组。更准确地说,pp 是一个长度为 nn 的数组,定义为 pi=a1+a2+…+aip_i = a_1 + a_2 + \ldots + a_i。随后,数组 pp 按非递减顺序排序。例如,若 a=[1,−1,−1,1,1]a = [1, -1, -1, 1, 1],则排序前 p=[1,0,−1,0,1]p = [1, 0, -1, 0, 1],排序后 p=[−1,0,0,1,1]p = [-1, 0, 0, 1, 1]。

你被给定排序后的前缀和数组 pp,但并不知道原始数组 aa 是什么。你的任务是计算满足上述过程能产生给定的已排序前缀和数组 pp 的初始数组 aa 的个数。由于该数目可能很大,你只需输出其对 998 244 353998\,244\,353 取模的结果。

输入格式

Each test contains multiple test cases. The first line contains a single integer tt (1≤t≤10001 \leq t \leq 1000) — the number of test cases. The description of the test cases follows.

The first line of each test case contains a single integer nn (1≤n≤50001 \le n \le 5000) — the size of the hidden array aa.

The second line of each test case contains nn integers p1,p2,…,pnp_1, p_2, \ldots, p_n (∣pi∣≤n|p_i| \le n) — the nn prefix sums of aa sorted in non-decreasing order.

It is guaranteed that p1≤p2≤…≤pnp_1 \le p_2 \le \ldots \le p_n.

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

每个测试包含多个测试用例。第一行包含一个整数 tt(1≤t≤10001 \leq t \leq 1000),表示测试用例的数量。随后是各测试用例的描述。

每个测试用例的第一行包含一个整数 nn(1≤n≤50001 \le n \le 5000),表示隐藏数组 aa 的大小。

每个测试用例的第二行包含 nn 个整数 p1,p2,…,pnp_1, p_2, \ldots, p_n(∣pi∣≤n|p_i| \le n),即数组 aa 的 nn 个前缀和按非递减顺序排序后的结果。

保证有 p1≤p2≤…≤pnp_1 \le p_2 \le \ldots \le p_n。

保证所有测试用例的 nn 值之和不超过 50005000。

输出格式

For each test case, output the answer modulo 998 244 353998\,244\,353.

对于每个测试用例,输出答案对 998 244 353998\,244\,353 取模的结果。

输入输出样例

  • 输入#1

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

    输出#1

    0
    1
    0
    3
    1

说明/提示

In the first two test cases, the only possible arrays aa for n=1n = 1 are a=[1]a = [1] and a=[−1]a = [-1]. Their respective sorted prefix sum arrays pp are p=[1]p = [1] and p=[−1]p = [-1]. Hence, there is no array aa that can result in the sorted prefix sum array p=[0]p = [0] and there is exactly 11 array aa that can result in the sorted prefix sum array p=[1]p = [1].

In the third test case, it can be proven that there is no array aa that could result in the sorted prefix sum array p=[−1,1,2]p = [-1, 1, 2].

In the fourth test case, the 33 possible arrays aa that could result in the sorted prefix sum array p=[−1,0,0,1,1]p = [-1, 0, 0, 1, 1] are:

  • a=[1,−1,1,−1,−1]a = [1, -1, 1, -1, -1]. The prefix sum array before sorting is p=[1,0,1,0,−1]p = [1, 0, 1, 0, -1], which after sorting gives p=[−1,0,0,1,1]p = [-1, 0, 0, 1, 1].
  • a=[1,−1,−1,1,1]a = [1, -1, -1, 1, 1]. The prefix sum array before sorting is p=[1,0,−1,0,1]p = [1, 0, -1, 0, 1], which after sorting gives p=[−1,0,0,1,1]p = [-1, 0, 0, 1, 1].
  • a=[−1,1,1,−1,1]a = [-1, 1, 1, -1, 1]. The prefix sum array before sorting is p=[−1,0,1,0,1]p = [-1, 0, 1, 0, 1], which after sorting gives p=[−1,0,0,1,1]p = [-1, 0, 0, 1, 1].

For the fifth test case, the only possible array aa that could result in the sorted prefix sum array p=[−4,−3,−3,−2,−1]p = [-4, -3, -3, -2, -1] is a=[−1,−1,−1,−1,1]a = [-1, -1, -1, -1, 1].

在前两个测试用例中,当 n=1n = 1 时,唯一可能的数组 aa 分别为 a=[1]a = [1] 和 a=[−1]a = [-1]。它们各自对应的排序后前缀和数组 pp 分别为 p=[1]p = [1] 和 p=[−1]p = [-1]。因此,不存在任何数组 aa 能够得到排序后的前缀和数组 p=[0]p = [0];而恰好存在 11 个数组 aa 能够得到排序后的前缀和数组 p=[1]p = [1]。

在第三个测试用例中,可以证明:不存在任何数组 aa 能够得到排序后的前缀和数组 p=[−1,1,2]p = [-1, 1, 2]。

在第四个测试用例中,能够得到排序后的前缀和数组 p=[−1,0,0,1,1]p = [-1, 0, 0, 1, 1] 的 33 个可能的数组 aa 为:

  • a=[1,−1,1,−1,−1]a = [1, -1, 1, -1, -1]。排序前的前缀和数组为 p=[1,0,1,0,−1]p = [1, 0, 1, 0, -1],排序后得到 p=[−1,0,0,1,1]p = [-1, 0, 0, 1, 1]。
  • a=[1,−1,−1,1,1]a = [1, -1, -1, 1, 1]。排序前的前缀和数组为 p=[1,0,−1,0,1]p = [1, 0, -1, 0, 1],排序后得到 p=[−1,0,0,1,1]p = [-1, 0, 0, 1, 1]。
  • a=[−1,1,1,−1,1]a = [-1, 1, 1, -1, 1]。排序前的前缀和数组为 p=[−1,0,1,0,1]p = [-1, 0, 1, 0, 1],排序后得到 p=[−1,0,0,1,1]p = [-1, 0, 0, 1, 1]。

在第五个测试用例中,唯一可能的数组 aa,使得其排序后的前缀和数组为 p=[−4,−3,−3,−2,−1]p = [-4, -3, -3, -2, -1],是 a=[−1,−1,−1,−1,1]a = [-1, -1, -1, -1, 1]。

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

首页