CF1738EBalance Addicts
CF1738EBalance Addicts
题目大意
有\(n\)个数的数列,把它分成若干个子集,保证所有子集的和能够组成一个回文数列,求\(mod\ 998244353\)后的方案数。
做法
思路
先分别从头和尾找到一段序列的和相同,记为\(l1\ r2\)然后从\(l1\ r2\)开始寻找一段最长的和相等的序列记为\(r1\ l2\)。
此时答案为
\]
注意
1、如果此时\(l1\ r2\)之间的数都为\(0\)。
显然:\(l1\ r2\)之间的数都可以随便选,那么将\(ans *= 2^{r2 - l1 + 1}\)即可退出
2、预处理\(sum1\ sum2\ tpow\)表示前缀和、后缀和、2的\(n\)次方
3、记得\(mod\ 998244353\)
code
#include<bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
const long long mod = 998244353;
int n;
long long ans , ans1 , sum1[N + 5] , sum2[N + 5] , fac[N + 5] , inv[N + 5] , tpow[N + 5] , a[N + 5];
long long pw(long long x , long long y) {
if(!y)
return 1;
long long z = pw (x , y / 2);
z = z * z % mod;
if(y & 1)
z = z * x % mod;
return z;
}
void pre() {
fac[1] = fac[0] = 1;
for(int i = 2 ; i <= N ; i++) {
fac[i] = fac[i - 1] * i % mod ;
}
inv[N] = pw (fac[N] , mod - 2);
for(int i = N - 1 ; i >= 0 ; i--) {
inv[i] = inv[i + 1] * (i + 1) % mod;
}
tpow[0] = 1;
for (int i = 1 ; i <= N ; i++)
tpow[i] = tpow[i - 1] * 2 % mod;
}
long long C(int x , int y) {
if(x<0||y<0) return 0;
if(x < y)
return 0;
return fac[x] * inv[y] % mod * inv[x - y] % mod;
}
int main () {
pre ();
int T;
scanf ("%d" , &T);
while (T--) {
ans = 1;
scanf ("%d" , &n);
for (int i = 1 ; i <= n ; i++) {
scanf ("%d" , &a[i]);
}
for (int i = 1 ; i <= n ; i++) {
sum1[i] = sum1[i - 1] + a[i];
}
for (int i = n - 1; i >= 1 ; i--) {
sum2[i] = sum2[i + 1] + a[i + 1];
}
for (int l1 = 1 , r2 = n - 1 , l2 , r1; l1 <= r2 ; l1 = r1 + 1 , r2 = l2 - 1) {
while (l1 <= r2 && sum1[l1] != sum2[r2]) {
if(sum1[l1] < sum2[r2])
l1 ++;
else
r2 --;
}
if (l1 > r2)
break;
if (sum1[l1] == sum1[r2]) {
ans = tpow[r2 - l1 + 1] * ans % mod;
break;
}
r1 = l1 , l2 = r2;
while (sum1[r1 + 1] == sum1[l1])
r1 ++;
while (sum2[l2 - 1] == sum2[r2])
l2 --;
ans1 = 0;
for (int i = 0 ; i <= min(r1 - l1 + 1 , r2 - l2 + 1) ; i++) {
ans1 = (ans1 + (C (r1 - l1 + 1 , i) * C (r2 - l2 + 1 , i) %mod) ) %mod;
}
ans = ans * ans1 % mod;
}
printf("%lld\n" , ans);
for (int i = 1 ; i <= n ; i++)
sum1[i] = sum2[i] = 0;
}
return 0;
}
CF1738EBalance Addicts的更多相关文章
- java运算符优先级记忆口诀
尊重原创:(口诀)转自http://lasombra.iteye.com/blog/991662 今天看到<java编程思想>中的运算符优先级助记口诀,不过"Ulcer Addi ...
- zoj 1010 (线段相交判断+多边形求面积)
链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=10 Area Time Limit: 2 Seconds Mem ...
- How good software makes us stupid?
How good software makes us stupid? 科技是怎样让人变傻的? People assume that iPhones, laptops and Netflix are e ...
- zoj 1010 Area【线段相交问题】
链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1010 http://acm.hust.edu.cn/vjudge/ ...
- L268 A terrifying look at the consequences of climate change
Climate change is a devilish problem for humanity: at once urgent and slow-moving, immediate and dis ...
- Why I Left the .NET Framework
The .NET Framework was good. Really good. Until it wasn't. Why did I leave .NET? In short, it constr ...
- 每日英语:How Often Do Gamblers Really Win?
The casino billboards lining America's roadways tantalize with the lure of riches. 'Easy Street. It' ...
- Social media users of the world unite!
Social media users of the world unite!全世界社交媒体用户联合起来!If Plato were alive today, he might well regard ...
- TEXT 8 Ready, fire, aim
TEXT 8 Ready, fire, aim 预备!开火!瞄准!! Feb 16th 2006 From The Economist print edition Foreword:A vice-pr ...
- 【256】◀▶IEW-答案
附答案 Unit I Fast food Model Answers: Model 1 The pie chart shows the fast foods that teenagers prefer ...
随机推荐
- 运行python脚本报错:selenium.common.exceptions.SessionNotCreatedException: Message: session not created
运行python脚本报错:selenium.common.exceptions.SessionNotCreatedException: Message: session not created 原因: ...
- 10.14 2020 实验 7:OpenDaylight 实验——Python 中的 REST API 调用
一.实验目的 对 Python 调用 OpenDaylight 的 REST API 方法有初步了解. 二.实验任务 本实验需要用另一种方法完成上一个实验相同的功能,即通过 Python 程序 ...
- find . -name "*.php" -execdir grep -nH --color=auto foo {} ';'
find . -name "*.php" -execdir grep -nH --color=auto foo {} ';'
- C# Nilakantha级数逼近PI算法
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- 【帆吖】Java学习零基础21
数组 1 package array; 2 3 public class Demo1 { 4 public static void main(String[] args) { 5 int[ ] num ...
- 安装DevExpress VCL,使用时报错 某单元文件找不到的解决办法
1.新建一个工程做为测试 2.点击projecct-->options 3. 4. 5.在上4图上的红框内加入packages文件 dxCoreRS27;dxGDIPlusRS27;dxComn ...
- 121、商城业务---订单服务---rabbitmq消息积压、丢失、重复等解决方案
- java的死锁与解决方法
一.什么是死锁? 死锁是指两个或两个以上的进程在执行过程中,由于竞争资源或者由于彼此通信而造成的一种阻塞的现象,若无外力作用,它们都将无限等待. 二.产生死锁的原因与四个条件 2.1 死锁原因 竞争资 ...
- 关与python面向对象的认识
面向对象编程 类:从一堆对象中以抽象的方式把相同的特征归类得到. 抽象类 类 实列 子类抽象为父类,子类继承父类特征. 类实例化为实例,实例抽象为类. class Human(object): cen ...
- Go_day02
Go基础语法 流程控制 一共有三种:顺序结构,选择结构,循环结构 if语句 /* if与else if的区别: 1:if无论是否满足条件都会向下执行,直到程序结束,else if 满足一个条件就会停止 ...