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 ...
随机推荐
- windows11中使用ctypes运行时出错:AttributeError: function *** not found
最近我在研究用ctypes实现python调用c,按照晚上的教程写下了类似下面的c程序: #include <stdio.h> int nn_test(int num){ printf(& ...
- nodejs 反单引号用法(·)
这个反单引号就是数字1旁边(~)下面的那个符号,平时用得很少,虽然单引号和双引号是使用较多的,但我们还有第三个方案,就是ES6中的模板字符串(反引号). 在nodejs中用反单引号(·)主要基于以下作 ...
- (十三).CSS3中的变换(transform),过渡(transition),动画(animation)
1 变换 transform 1.1 变换相关 CSS 属性 CSS 属性名 含义 值 transform 设置变换方式 transform-origin 设置变换的原点 使用关键字或坐标设置位置 t ...
- 优先使用C++的别名声明(using)来替换typedef
C++98中,我们如果想用简写的方式表达一个类型,那么可以使用typedef关键字: typedef std::unique_ptr<std::unordered_map<std::str ...
- C# async、await、Task 探讨
test02.ProntOut(); ///*第五题*/ static class test02 { public static async void ProntOut() { Console.Wri ...
- Oracle 取Group By 第一条
select *from (select emp.*,row_number() over(partition by deptno order by rownum) cn from emp)where ...
- 前端访问Tornado跨域问题解决
- Python使用Eel和HTML开发桌面应用GUI直接用web前端的VUE+VANT来做
python的gui太难用了,唯一能配置独立前端的程序只有web.所以用web做前端,到python,完美! 环境准备 Python 3.9 Chrome浏览器(由于Eel是直接调用的Ch ...
- 视频播放-videojs
视频播放-video-js组件 安装 yarn add video.js --save npm install video.js --save 代码 import React, { useEffect ...
- 如何加密一个sheel脚本!
脚本写完后,如果要发布给其它人使用的话,可能会因安全原因而受阻,特别是脚本中包含密码等原因,而对脚本加密则可以解决此问题,本文提供了CentOS7/8环境下,加密shell脚本需要安装的程序和方法. ...