[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 ...
随机推荐
- mysql-router-MIC-8.0.26集群部署
1.具体部署详情请看视频 https://space.bilibili.com/677825194 2.mysql主要配置如下 cat > /etc/my.cnf <<EOF [cl ...
- Ubuntu20.04安装 maven并配置阿里源
Ubuntu20.04安装 maven并配置阿里源 sudo apt update sudo apt install maven #安装maven,默认安装路径为/usr/share/maven 添加 ...
- The type name or alias SqlServer could not be resolved.Please check your configuration
The type name or alias SqlServer could not be resolved.Please check your configuration file.... 检查一下 ...
- SpringBoot入门07-Thymeleaf中显示ajax请求到的数据
Thymeleaf中显示ajax请求所需依赖 <!--所需依赖--><dependency> <groupId>org.springframework.boot&l ...
- TCP三次握手四次挥手,通俗易懂版
三次握手四次挥手 三次握手 其实很好理解,三次握手就是保证双手都有发送和接受的能力.那么最少三次才能验证完成 即----> 客户端发送---服务端收到----服务端发送-- 1.客户端发送 -- ...
- 搭载Dubbo+Zookeeper踩了这么多坑,我终于决定写下这篇!
大家好,我是melo,一名大二上软件工程在读生,经历了一年的摸滚,现在已经在工作室里边准备开发后台项目啦. 这篇文章我们不谈数据结构了,来谈谈入门分布式踩过的坑.感觉到了分布式这一层,由于技术更新迭代 ...
- 【数据结构与算法Python版学习笔记】算法分析
什么是算法分析 算法是问题解决的通用的分步的指令的聚合 算法分析主要就是从计算资源的消耗的角度来评判和比较算法. 计算资源指标 存储空间或内存 执行时间 影响算法运行时间的其他因素 分为最好.最差和平 ...
- Convolutional Neural Network-week2编程题2(Residual Networks)
1. Residual Networks(残差网络) 残差网络 就是为了解决深网络的难以训练的问题的. In this assignment, you will: Implement the basi ...
- the Agiles Scrum Meeting 9
会议时间:2020.4.17 20:00 1.每个人的工作 今天已完成的工作 个人结对项目增量开发组:基本实现个人项目创建.仓库自动分配,修复bug issues: 增量组:准备评测机制,增加仓库自动 ...
- CentOS 7:快速安装Tomcat7.x
到官网下载对应的压缩包,CentOS 7的Tomcat下载地址:http://tomcat.apache.org/download-70.cgi,下载后传进服务器中并放在你指定的位置上. 或者使用命令 ...