A16388 | Counting Prefixes
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
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 $p_i = a_1 + a_2 + \ldots + a_i$ . 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 $998\,244\,353$ .
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 $998\,244\,353$ .
输入格式
Each test contains multiple test cases. The first line contains a single integer $t$ ( $1 \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 $n$ ( $1 \le n \le 5000$ ) — the size of the hidden array $a$ .
The second line of each test case contains $n$ integers $p_1, p_2, \ldots, p_n$ ( $|p_i| \le n$ ) — the $n$ prefix sums of $a$ sorted in non-decreasing order.
It is guaranteed that $p_1 \le p_2 \le \ldots \le p_n$ .
It is guaranteed that the sum of $n$ over all test cases does not exceed $5000$ .
The first line of each test case contains a single integer $n$ ( $1 \le n \le 5000$ ) — the size of the hidden array $a$ .
The second line of each test case contains $n$ integers $p_1, p_2, \ldots, p_n$ ( $|p_i| \le n$ ) — the $n$ prefix sums of $a$ sorted in non-decreasing order.
It is guaranteed that $p_1 \le p_2 \le \ldots \le p_n$ .
It is guaranteed that the sum of $n$ over all test cases does not exceed $5000$ .
输出格式
For each test case, output the answer modulo $998\,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 $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]$ .
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]$ .
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted