CF1738E.Balance Addicts

提高+/省选-

通过率:0%

时间限制:2.00s

内存限制:512MB

AC君温馨提醒

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

题目描述

Given an integer sequence a1,a2,…,ana_1, a_2, \dots, a_n of length nn, your task is to compute the number, modulo 998244353998244353, of ways to partition it into several non-empty continuous subsequences such that the sums of elements in the subsequences form a balanced sequence.

A sequence s1,s2,…,sks_1, s_2, \dots, s_k of length kk is said to be balanced, if si=sk−i+1s_{i} = s_{k-i+1} for every 1≤i≤k1 \leq i \leq k. For example, [1,2,3,2,1][1, 2, 3, 2, 1] and [1,3,3,1][1,3,3,1] are balanced, but [1,5,15][1,5,15] is not.

Formally, every partition can be described by a sequence of indexes i1,i2,…,iki_1, i_2, \dots, i_k of length kk with 1=i1<i2<⋯<ik≤n1 = i_1 \lt i_2 \lt \dots \lt i_k \leq n such that

  1. kk is the number of non-empty continuous subsequences in the partition;
  2. For every 1≤j≤k1 \leq j \leq k, the jj-th continuous subsequence starts with aija_{i_j}, and ends exactly before aij+1a_{i_{j+1}}, where ik+1=n+1i_{k+1} = n + 1. That is, the jj-th subsequence is aij,aij+1,…,aij+1−1a_{i_j}, a_{i_j+1}, \dots, a_{i_{j+1}-1}.

There are 2n−12^{n-1} different partitions in total.

Let s1,s2,…,sks_1, s_2, \dots, s_k denote the sums of elements in the subsequences with respect to the partition i1,i2,…,iki_1, i_2, \dots, i_k. Formally, for every 1≤j≤k1 \leq j \leq k, $$ s_j = \sum_{i=i_{j}}^{i_{j+1}-1} a_i = a_{i_j} + a_{i_j+1} + \dots + a_{i_{j+1}-1}. $$ For example, the partition [1 ∣ 2,3 ∣ 4,5,6][1\,|\,2,3\,|\,4,5,6] of sequence [1,2,3,4,5,6][1,2,3,4,5,6] is described by the sequence [1,2,4][1,2,4] of indexes, and the sums of elements in the subsequences with respect to the partition is [1,5,15][1,5,15].

Two partitions i1,i2,…,iki_1, i_2, \dots, i_k and i1′,i2′,…,ik′′i'_1, i'_2, \dots, i'_{k'} (described by sequences of indexes) are considered to be different, if at least one of the following holds.

  • k≠k′k \neq k',
  • ij≠ij′i_j \neq i'_j for some 1 \leq j \leq \min\left{ k, k' \right}.

给定一个长度为 nn 的整数序列 a1,a2,…,ana_1, a_2, \dots, a_n,你的任务是计算将其划分为若干个非空连续子序列的方式数目(对 998244353998244353 取模),使得这些子序列的元素和构成一个平衡序列。

一个长度为 kk 的序列 s1,s2,…,sks_1, s_2, \dots, s_k 被称为平衡序列,当且仅当对每个 1≤i≤k1 \leq i \leq k,均有 si=sk−i+1s_{i} = s_{k-i+1}。例如,[1,2,3,2,1][1, 2, 3, 2, 1] 和 [1,3,3,1][1,3,3,1] 是平衡序列,但 [1,5,15][1,5,15] 不是。

形式化地,每个划分可由一个长度为 kk 的下标序列 i1,i2,…,iki_1, i_2, \dots, i_k 描述,其中 1=i1<i2<⋯<ik≤n1 = i_1 \lt i_2 \lt \dots \lt i_k \leq n,满足:

  1. kk 表示该划分中非空连续子序列的个数;
  2. 对每个 1≤j≤k1 \leq j \leq k,第 jj 个连续子序列起始于 aija_{i_j},并恰好终止于 aij+1−1a_{i_{j+1}-1},其中定义 ik+1=n+1i_{k+1} = n + 1。即,第 jj 个子序列为 aij,aij+1,…,aij+1−1a_{i_j}, a_{i_j+1}, \dots, a_{i_{j+1}-1}。

总共有 2n−12^{n-1} 种不同的划分方式。

令 s1,s2,…,sks_1, s_2, \dots, s_k 表示在划分 i1,i2,…,iki_1, i_2, \dots, i_k 下各子序列的元素和。形式化地,对每个 1≤j≤k1 \leq j \leq k,有

s_j=sum_i=i_ji_j+1−1a_i=a_i_j+a_i_j+1+dots+a_i_j+1−1.s\_j = \\sum\_{i=i\_{j}}^{i\_{j+1}-1} a\_i = a\_{i\_j} + a\_{i\_j+1} + \\dots + a\_{i\_{j+1}-1}.

例如,序列 [1,2,3,4,5,6][1,2,3,4,5,6] 的划分 [1 ∣ 2,3 ∣ 4,5,6][1\,|\,2,3\,|\,4,5,6] 由下标序列 [1,2,4][1,2,4] 描述,对应子序列的元素和为 [1,5,15][1,5,15]。

两个划分 i1,i2,…,iki_1, i_2, \dots, i_k 和 i1′,i2′,…,ik′′i'_1, i'_2, \dots, i'_{k'}(以各自的下标序列描述)被视为不同,当且仅当以下任一条件成立:

  • k≠k′k \neq k',
  • 存在某个 1≤j≤min⁡{k,k′}1 \leq j \leq \min\left\{ k, k' \right\},使得 ij≠ij′i_j \neq i'_j。

输入格式

Each test contains multiple test cases. The first line contains an integer tt (1≤t≤1051 \leq t \leq 10^5) — the number of test cases. The following lines contain the description of each test case.

The first line of each test case contains an integer nn (1≤n≤1051 \leq n \leq 10^5), indicating the length of the sequence aa.

The second line of each test case contains nn integers a1,a2,…,ana_1, a_2, \dots, a_n (0≤ai≤1090 \leq a_i \leq 10^9), indicating the elements of the sequence aa.

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

每个测试包含多个测试用例。第一行包含一个整数 tt(1≤t≤1051 \leq t \leq 10^5),表示测试用例的数量。接下来的若干行描述各个测试用例。

每个测试用例的第一行包含一个整数 nn(1≤n≤1051 \leq n \leq 10^5),表示序列 aa 的长度。

每个测试用例的第二行包含 nn 个整数 a1,a2,…,ana_1, a_2, \dots, a_n(0≤ai≤1090 \leq a_i \leq 10^9),表示序列 aa 的各元素。

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

输出格式

For each test case, output the number of partitions with respect to which the sum of elements in each subsequence is balanced, modulo 998244353998244353.

对于每个测试用例,输出满足每段子序列元素和均相等的划分方案数,对 998244353998244353 取模。

输入输出样例

  • 输入#1

    6
    1
    1000000000
    2
    1 1
    4
    0 0 1 0
    5
    1 2 3 2 1
    5
    1 3 5 7 9
    32
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

    输出#1

    1
    2
    3
    4
    2
    150994942

说明/提示

For the first test case, there is only one way to partition a sequence of length 11, which is itself and is, of course, balanced.

For the second test case, there are 22 ways to partition it:

  • The sequence [1,1][1, 1] itself, then s=[2]s = [2] is balanced;
  • Partition into two subsequences [1 ∣ 1][1\,|\,1], then s=[1,1]s = [1, 1] is balanced.

For the third test case, there are 33 ways to partition it:

  • The sequence [0,0,1,0][0, 0, 1, 0] itself, then s=[1]s = [1] is balanced;
  • [0 ∣ 0,1 ∣ 0][0 \,|\, 0, 1 \,|\, 0], then s=[0,1,0]s = [0, 1, 0] is balanced;
  • [0,0 ∣ 1 ∣ 0][0, 0 \,|\, 1 \,|\, 0], then s=[0,1,0]s = [0, 1, 0] is balanced.

For the fourth test case, there are 44 ways to partition it:

  • The sequence [1,2,3,2,1][1, 2, 3, 2, 1] itself, then s=[9]s = [9] is balanced;
  • [1,2 ∣ 3 ∣ 2,1][1, 2 \,|\, 3 \,|\, 2, 1], then s=[3,3,3]s = [3, 3, 3] is balanced;
  • [1 ∣ 2,3,2 ∣ 1][1 \,|\, 2, 3, 2 \,|\, 1], then s=[1,7,1]s = [1, 7, 1] is balanced;
  • [1 ∣ 2 ∣ 3 ∣ 2 ∣ 1][1 \,|\, 2 \,|\, 3 \,|\, 2 \,|\, 1], then s=[1,2,3,2,1]s = [1, 2, 3, 2, 1] is balanced.

For the fifth test case, there are 22 ways to partition it:

  • The sequence [1,3,5,7,9][1, 3, 5, 7, 9] itself, then s=[25]s = [25] is balanced;
  • [1,3,5 ∣ 7 ∣ 9][1, 3, 5 \,|\, 7 \,|\, 9], then s=[9,7,9]s = [9, 7, 9] is balanced.

For the sixth test case, every possible partition should be counted. So the answer is 232−1≡150994942(mod998244353)2^{32-1} \equiv 150994942 \pmod {998244353}.

对于第一个测试用例,长度为 11 的序列只有一种划分方式,即其自身,而该序列显然是平衡的。

对于第二个测试用例,共有 22 种划分方式:

  • 序列 [1,1][1, 1] 本身,则 s=[2]s = [2] 是平衡的;
  • 划分为两个子序列 [1 ∣ 1][1\,|\,1],则 s=[1,1]s = [1, 1] 是平衡的。

对于第三个测试用例,共有 33 种划分方式:

  • 序列 [0,0,1,0][0, 0, 1, 0] 本身,则 s=[1]s = [1] 是平衡的;
  • [0 ∣ 0,1 ∣ 0][0 \,|\, 0, 1 \,|\, 0],则 s=[0,1,0]s = [0, 1, 0] 是平衡的;
  • [0,0 ∣ 1 ∣ 0][0, 0 \,|\, 1 \,|\, 0],则 s=[0,1,0]s = [0, 1, 0] 是平衡的。

对于第四个测试用例,共有 44 种划分方式:

  • 序列 [1,2,3,2,1][1, 2, 3, 2, 1] 本身,则 s=[9]s = [9] 是平衡的;
  • [1,2 ∣ 3 ∣ 2,1][1, 2 \,|\, 3 \,|\, 2, 1],则 s=[3,3,3]s = [3, 3, 3] 是平衡的;
  • [1 ∣ 2,3,2 ∣ 1][1 \,|\, 2, 3, 2 \,|\, 1],则 s=[1,7,1]s = [1, 7, 1] 是平衡的;
  • [1 ∣ 2 ∣ 3 ∣ 2 ∣ 1][1 \,|\, 2 \,|\, 3 \,|\, 2 \,|\, 1],则 s=[1,2,3,2,1]s = [1, 2, 3, 2, 1] 是平衡的。

对于第五个测试用例,共有 22 种划分方式:

  • 序列 [1,3,5,7,9][1, 3, 5, 7, 9] 本身,则 s=[25]s = [25] 是平衡的;
  • [1,3,5 ∣ 7 ∣ 9][1, 3, 5 \,|\, 7 \,|\, 9],则 s=[9,7,9]s = [9, 7, 9] 是平衡的。

对于第六个测试用例,应统计所有可能的划分方式。因此答案为 232−1≡150994942(mod998244353)2^{32-1} \equiv 150994942 \pmod {998244353}。

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

首页