CF1919E.Counting Prefixes
普及/提高-
通过率:0%
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
There is a hidden array a of size n consisting of only 1 and −1 . Let p be the prefix sums of array a . More formally, p is an array of length n defined as pi=a1+a2+…+ai . Afterwards, array p is sorted in non-decreasing order. For example, if a=[1,−1,−1,1,1] , then p=[1,0,−1,0,1] before sorting and p=[−1,0,0,1,1] after sorting.
You are given the prefix sum array p after sorting, but you do not know what array a is. Your task is to count the number of initial arrays a such that the above process results in the given sorted prefix sum array p . As this number can be large, you are only required to find it modulo 998244353 .
输入格式
Each test contains multiple test cases. The first line contains a single integer t ( 1≤t≤1000 ) — the number of test cases. The description of the test cases follows.
The first line of each test case contains a single integer n ( 1≤n≤5000 ) — the size of the hidden array a .
The second line of each test case contains n integers p1,p2,…,pn ( ∣pi∣≤n ) — the n prefix sums of a sorted in non-decreasing order.
It is guaranteed that p1≤p2≤…≤pn .
It is guaranteed that the sum of n over all test cases does not exceed 5000 .
输出格式
For each test case, output the answer modulo 998244353 .
输入输出样例
输入#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 a for n=1 are a=[1] and a=[−1] . Their respective sorted prefix sum arrays p are p=[1] and p=[−1] . Hence, there is no array a that can result in the sorted prefix sum array p=[0] and there is exactly 1 array a that can result in the sorted prefix sum array p=[1] .
In the third test case, it can be proven that there is no array a that could result in the sorted prefix sum array p=[−1,1,2] .
In the fourth test case, the 3 possible arrays a that could result in the sorted prefix sum array p=[−1,0,0,1,1] are:
- a=[1,−1,1,−1,−1] . The prefix sum array before sorting is p=[1,0,1,0,−1] , which after sorting gives p=[−1,0,0,1,1] .
- a=[1,−1,−1,1,1] . The prefix sum array before sorting is p=[1,0,−1,0,1] , which after sorting gives p=[−1,0,0,1,1] .
- a=[−1,1,1,−1,1] . The prefix sum array before sorting is p=[−1,0,1,0,1] , which after sorting gives p=[−1,0,0,1,1] .
For the fifth test case, the only possible array a that could result in the sorted prefix sum array p=[−4,−3,−3,−2,−1] is a=[−1,−1,−1,−1,1] .