AtCoder Beginner Contest 098 D - Xor Sum 2
2024-08-29 20:31:37
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
Copy
4
2 5 4 6
Sample Output 1
Copy
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
Copy
9
0 0 0 0 0 0 0 0 0
Sample Output 2
Copy
45
Sample Input 3
Copy
19
885 8 1 128 83 32 256 206 639 16 4 128 689 32 8 64 885 969 1
Sample Output 3
Copy
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 ;
}
最新文章
- Reversing Linked List
- iOS知识总结
- oracle select into 的时候提示未找到数据
- HTML编码规则、CSS属性声明顺序--简介
- spring mvc配置文件dispatcher-servlet.xml详解
- MVC使用的MetaModel代码生成器模板
- platform平台设备驱动简化示例代码
- window FILES——windows文件管理相关实例
- 201521123059 《Java程序设计》第七周学习总结
- Checksum软件的简单设计
- python使用随机的163账号发送邮件
- $Django python中使用redis, django中使用(封装了),redis开启事务(管道)
- Top值
- canvas简易画板。
- CentOS 与 Ubuntu 使用命令搭建 LAMP 环境
- Kali linux创建和删除用户
- Nuget CsvHelper 的使用
- uva1659(最大费用循环流)
- Oracle EBS PO rcv_shipment_headers 数据缺失
- 深入源码分析Java线程池的实现原理