不妨先操作一轮,使得$0\le a_{i}\le 2$

结论:若序列中存在1,则答案为0或1

考虑归纳,注意到若序列中存在1,除非所有元素均为1,否则操作一轮后必然仍存在1,那么根据归纳假设即成立,而当所有元素均为1时,显然答案一定为0或1(序列长度已经为1),同样成立

由此,实际上只需要通过奇偶性即可确定答案,而注意到$|x-y|\equiv x+y(mod\ 2)$,因此不妨将转移的式子变为$a'_{i}=a_{i}+a_{i+1}$(模2意义下)

简单计数,不难发现$a_{i}$对答案的贡献即${n-1\choose i-1}a_{i}$,求出其模2的值并相加即可

另外,如果序列中不存在1,分类讨论:

1.若序列中不存在2,显然答案即为0

2.若序列中存在2,不妨将所有$a_{i}$除以2后求出答案,再将答案乘上2即可

时间复杂度为$o(n)$,可以通过

 1 #include<bits/stdc++.h>
2 using namespace std;
3 #define N 1000005
4 int n,a[N];
5 char s[N];
6 int C(int n,int m){
7 return (n&m)==m;
8 }
9 int calc(){
10 int ans=0;
11 for(int i=1;i<=n;i++)
12 if (a[i]&1)ans^=C(n-1,i-1);
13 return ans;
14 }
15 int main(){
16 scanf("%d%s",&n,s+1);
17 n--;
18 for(int i=1;i<=n;i++)a[i]=abs((int)s[i]-(int)s[i+1]);
19 bool flag=0;
20 for(int i=1;i<=n;i++)
21 if (a[i]==1)flag=1;
22 if (flag)printf("%d\n",calc());
23 else{
24 flag=0;
25 for(int i=1;i<=n;i++)
26 if (a[i]==2)flag=1;
27 if (!flag)printf("0\n");
28 else{
29 for(int i=1;i<=n;i++)a[i]>>=1;
30 printf("%d\n",(calc()<<1));
31 }
32 }
33 return 0;
34 }

最新文章

  1. C++设计模式-Strategy策略模式
  2. diff和common
  3. html页面元素加载顺序
  4. 好!maximum-product-of-word-lengths
  5. 兼容IE,chrome 等所有浏览器 回到顶部代码
  6. ubuntu12.04 安装 chrome
  7. T4模板试水篇1_入门
  8. a^b的前n位数
  9. Qt入门-字符串类QString
  10. 软件測试系统文章(文件夹&amp;amp;链接在此)
  11. Mybatis(三)返回值
  12. 【Unity Shaders】使用Unity Render Textures实现画面特效——建立画面特效脚本系统
  13. scrollview中edittext失去焦点问题
  14. OC字典的使用
  15. JAVA中将对象转为Map类型
  16. linux 基本操作centos7
  17. A - Class Statistics
  18. 633. Sum of Square Numbers
  19. 使用Hexo和Github Pages快速搭建个人博客
  20. css清除浮动clearfix:after的用法详解

热门文章

  1. 在开源项目或项目中使用git建立fork仓库
  2. 倒计时 | 7.24 阿里云 Serverless Developer Meetup 杭州站报名火热进行中!
  3. Linux查找运行程序主目录
  4. 题解 Weak in the Middle
  5. Kubernetes-Service介绍(二)-服务发现
  6. Redis:学习笔记-04
  7. Scrum Meeting 0522
  8. seata整合多数据源
  9. Spring Authorization Server的使用
  10. FastAPI 学习之路(五十六)将token存放在redis