[atAGC043B]123 Triangle
不妨先操作一轮,使得$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 }
[atAGC043B]123 Triangle的更多相关文章
- 二分--1043 - Triangle Partitioning
1043 - Triangle Partitioning PDF (English) Statistics Forum Time Limit: 0.5 second(s) Memory Limit: ...
- 1043 - Triangle Partitioning(数学)
1043 - Triangle Partitioning PDF (English) Statistics Forum Time Limit: 0.5 second(s) Memory Limit ...
- Entity Framework 6 Recipes 2nd Edition(12-3)译 -> 数据库连接日志
12-3. 数据库连接日志 问题 你想为每次与数据库的连接和断开记录日志 解决方案 EF为DbContext的连接公开了一个StateChange 事件.我们需要处理这个事件, 为每次与数据库的连接和 ...
- [LeetCode] Triangle 三角形
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...
- [LeetCode] Pascal's Triangle II 杨辉三角之二
Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return [1,3, ...
- [LeetCode] Pascal's Triangle 杨辉三角
Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Retur ...
- 【leetcode】Pascal's Triangle II
题目简述: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Retur ...
- 【leetcode】Pascal's Triangle
题目简述: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5 ...
- POJ 1163 The Triangle(简单动态规划)
http://poj.org/problem?id=1163 The Triangle Time Limit: 1000MS Memory Limit: 10000K Total Submissi ...
随机推荐
- vue3 element-plus 配置json快速生成table列表组件,提升生产力近500%(已在公司使用,持续优化中)
️本文为博客园首发文章,未获授权禁止转载 大家好,我是aehyok,一个住在深圳城市的佛系码农♀️,如果你喜欢我的文章,可以通过点赞帮我聚集灵力️. 个人github仓库地址: https:gith ...
- Spirit带你了解如何安全引入第三方资源
Spirit带你了解如何安全的引入第三方资源 本文介绍一下如何安全的引入第三方资源 同源策略(SOP) 首先我们来了解一下什么是同源策略,下面的是wiki百科的定义 同源策略是指Web浏览器中,允许某 ...
- DOS命令和快捷键
- Redis 高阶数据类型重温
今天这个专题接着上一篇 Redis 的基本数据类型 继续讲解剩下的高阶数据类型:BitMap.HyperLogLog 和 GEO hash.这些数据结构的底层也都是基于我们前面说的 5 种 基本类型, ...
- 热身训练1 ping ping ping
点此进入 题意: 一棵树,n+1 个节点,以0号节点为根,给出端点(a,b),节点a到节点b的路径上,至少有一个点是"坏掉的",求"坏掉的点"最少 分析: St ...
- Python课程笔记(十一)
一.线程与多线程 1.线程与进程 线程指的是 进程(运行中的程序)中单一顺序的执行流. 多个独立执行的线程相加 = 一个进程 多线程程序是指一个程序中包含有多个执行流,多线程是实现并发机制的一种有效手 ...
- 与 Python 之父聊天:更快的 Python!
Python猫注: 在今年 5 月的 Python 语言峰会上,Guido van Rossum 作了一场<Making CPython Faster>的分享(材料在此),宣告他加入了激动 ...
- Swift-方法调度-类的普通方法底层探究
1. 类的普通方法调度 写一个结构体和一个类,对比看看方法调用的方式: // 结构体 struct PersonStruct { func changClassName() {} } let s = ...
- vs2015 MSB600 "inf2cat.ext"已退出,代码为2
使用vs2015编译XDMA驱动过程中,报如下错误: vs2015 MSB600 "inf2cat.ext"已退出,代码为2 在使用Qt编译PCIE码表的过程中,出现C1038:无 ...
- TypeError: 'encoding' is an invalid keyword argument for this function 解决Python 2.7
在python2.7中这样调用代码 open('file/name.txt','r',encoding= 'utf-8').read() 会出现 TypeError: 'encoding' is an ...