AtCoder Beginner Contest 098 D - Xor Sum 2
D - Xor Sum 2
Time limit : 2sec / Memory limit : 1024MB
Score : 500 points
Problem Statement
There is an integer sequence A of length N.
Find the number of the pairs of integers l and r (1≤l≤r≤N) that satisfy the following condition:
- Al xor Al+1 xor … xor Ar=Al + Al+1 + … + Ar
Here, xor denotes the bitwise exclusive OR.
Definition of XOR
Constraints
- 1≤N≤2×105
- 0≤Ai<220
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N
A1 A2 … AN
Output
Print the number of the pairs of integers l and r (1≤l≤r≤N) that satisfy the condition.
Sample Input 1
4
2 5 4 6
Sample Output 1
5
(l,r)=(1,1),(2,2),(3,3),(4,4) clearly satisfy the condition. (l,r)=(1,2) also satisfies the condition, since A1 xor A2=A1 + A2=7. There are no other pairs that satisfy the condition, so the answer is 5.
Sample Input 2
9
0 0 0 0 0 0 0 0 0
Sample Output 2
45
Sample Input 3
19
885 8 1 128 83 32 256 206 639 16 4 128 689 32 8 64 885 969 1
Sample Output 3
37
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <string>
#include <deque>
using namespace std;
#define ll long long
#define N 200009
#define gep(i,a,b) for(int i=a;i<=b;i++)
#define gepp(i,a,b) for(int i=a;i>=b;i--)
#define gep1(i,a,b) for(ll i=a;i<=b;i++)
#define gepp1(i,a,b) for(ll i=a;i>=b;i--)
#define mem(a,b) memset(a,b,sizeof(a))
int n,a[N];
ll sum[N],b[N];
/*
要想相加和等于异或和,就要区间的数二进制的每一位1只有一个
例如 1 2 4 8
如果 [l,r] 不可以,那么l++
如果[l,r] 可以,那么 r++
*/
int main()
{
scanf("%d",&n);
gep(i,,n) {
scanf("%d",&a[i]);
sum[i]=sum[i-]+a[i];
b[i]=b[i-]^a[i];
}
int l=;
ll ans=;
gep(r,,n){
for(;sum[r]-sum[l-]!=(b[r]^b[l-]);l++);//!=的优先级大于^,因此要加()
ans+=(r-l+);
//[l,r] :每一次的可以就是多了[l,r],[l+1,r],[l+2,r]……[r,r]共(r-l+1)个区间
//可以一定是连续的,一旦不可以了就要改变r,l
}
printf("%lld\n",ans);
return ;
}
AtCoder Beginner Contest 098 D - Xor Sum 2的更多相关文章
- AtCoder Regular Contest 098 D - Xor Sum 2 区间异或=相加 DP思想
题意:给出n个数,求它的连续子序列中,满足下列公式,(l,r)的对数有多少对 Al xor Al+1 xor … xor Ar=Al + Al+1 + … + Ar 思路:由题意可以得到,连续子序列, ...
- Atcoder Beginner Contest 121 D - XOR World(区间异或和)
题目链接:https://atcoder.jp/contests/abc121/tasks/abc121_d 题目很裸(Atcoder好像都比较裸 就给一个区间求异或和 n到1e12 肯定不能O(n) ...
- AtCoder Beginner Contest 179 E - Sequence Sum (模拟)
题意:\(f(x,m)\)表示\(x\ mod\ m\),\(A_{1}=1\),而\(A_{n+1}=f(A^{2}_{n},M)\),求\(\sum^{n}_{i=1}A_{i}\). 题解:多算 ...
- AtCoder Beginner Contest 146_E - Rem of Sum is Num
预处理即可 我们要找的是 (f[i] - f[j]) % k == i - j 移项可得 f[i] - i = f[j] - j 在 i - j <= k 的条件下 因此题目变成了,对于每个右端 ...
- AtCoder Regular Contest 098
AtCoder Regular Contest 098 C - Attention 题意 给定一个只包含"E","W"字符串,可以花一的花费使他们互相转换.选定 ...
- AtCoder Beginner Contest 100 2018/06/16
A - Happy Birthday! Time limit : 2sec / Memory limit : 1000MB Score: 100 points Problem Statement E8 ...
- AtCoder Beginner Contest 053 ABCD题
A - ABC/ARC Time limit : 2sec / Memory limit : 256MB Score : 100 points Problem Statement Smeke has ...
- AtCoder Beginner Contest 137 F
AtCoder Beginner Contest 137 F 数论鬼题(虽然不算特别数论) 希望你在浏览这篇题解前已经知道了费马小定理 利用用费马小定理构造函数\(g(x)=(x-i)^{P-1}\) ...
- AtCoder Beginner Contest 154 题解
人生第一场 AtCoder,纪念一下 话说年后的 AtCoder 比赛怎么这么少啊(大雾 AtCoder Beginner Contest 154 题解 A - Remaining Balls We ...
随机推荐
- HDU-1003:Max Sum(优化)
Max Sum Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Sub ...
- 转 PHP函数---$_Get()和$_Post()的用法
一.$_Get()和$_Post()函数是用来传值的,即对应两种提交表单的方法,get和post. 二.$_Get方法 (1)获取通过URL的传值 Example 1 新建两个PHP文件,1.php, ...
- JAVA常用知识总结(七)——Spring
如果一个接口有2个不同的实现, 如何Autowire某一个指定的实现? 1.通过增加@Qualifier(实现类的名字): @Autowired @Qualifier("GirlStuden ...
- 05.Javascript——入门函数
//定义函数的方法1 function abs(x) { if (x >= 0) { return x; } else { return -x; } } 上述abs()函数的定义如下: func ...
- #118. 【UR #8】赴京赶考
链接:#118. [UR #8]赴京赶考 高中,高中,短暂的三年.NOI是高中结业考试,而高考在每年暑假举行. 高二暑假,这是你最后一次参加高考的机会.你已经为了高考停课很久了,OI的知识很久没管了. ...
- 安卓6.0之前的系统 判断app是否有录音权限
public static synchronized boolean isVoicePermission() { AudioRecord record = null; try { record = n ...
- 重置Cacti密码
Cacti登录密码忘记,重置Cacti密码 用root用户进入系统 [root@localhsot]# mysql -u root -p mysql> show databases; mysql ...
- CSS-学习笔记五
1. 权重: 内联:A ID:B Class:C 标签:D 继承:0 2. 文字阴影text-shadow 3. 文字缩进text-index 4. 文本换行 5. 文本溢出 6. 圆角 ...
- codevs 1097 校门外的树 2005年NOIP全国联赛普及组 (线段树)
时间限制: 1 s 空间限制: 128000 KB 题目等级 : 白银 Silver 题目描述 Description 某校大门外长度为L的马路上有一排树,每两棵相邻的树之间的间隔都是1米.我们可 ...
- Java学习之初识线程
“身之主宰便是心,心之所发便是意,意之本体便是知,意之所在便是物 --摘自阳明先生语录” 1.概念 在说线程之前我们先了解关于进程的一些知识,什么是进程? 程序一旦运行就是一个独立的进程,以windo ...