ARC 098 D - Xor Sum 2
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
发现 xor 0是没有影响的,所以可以把0忽视掉(用一个链表一样的东西)。
因为一旦有两个数 a[i] & a[j] != 0,那么就是不满足的,所以不算0的话长度一定不超过20,所以直接暴力做就行了。
#include<cstring>
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<ctime>
#define ll long long
using namespace std;
const int maxn=200005; int n,a[maxn],lef[maxn];
ll ans=0; int main(){
scanf("%d",&n);
for(int i=1,las=0;i<=n;i++){
scanf("%d",a+i),lef[i]=las;
if(a[i]) las=i;
} for(int i=1,now,j;i<=n;i++){
now=a[i]; for(j=lef[i];j;now^=a[j],j=lef[j]) if(now&a[j]) break; ans+=(ll)(i-j); } cout<<ans<<endl;
return 0;
}
ARC 098 D - Xor Sum 2的更多相关文章
- AtCoder Beginner Contest 098 D - Xor Sum 2
D - Xor Sum 2 Time limit : 2sec / Memory limit : 1024MB Score : 500 points Problem Statement There i ...
- AtCoder Regular Contest 098 D - Xor Sum 2 区间异或=相加 DP思想
题意:给出n个数,求它的连续子序列中,满足下列公式,(l,r)的对数有多少对 Al xor Al+1 xor … xor Ar=Al + Al+1 + … + Ar 思路:由题意可以得到,连续子序列, ...
- HDU 4825 Xor Sum(经典01字典树+贪心)
Xor Sum Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 132768/132768 K (Java/Others) Total ...
- 字典树-百度之星-Xor Sum
Xor Sum Problem Description Zeus 和 Prometheus 做了一个游戏,Prometheus 给 Zeus 一个集合,集合中包括了N个正整数,随后 Prometheu ...
- HDU 4825 Xor Sum 字典树+位运算
点击打开链接 Xor Sum Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 132768/132768 K (Java/Others) ...
- 2014百度之星第三题Xor Sum(字典树+异或运算)
Xor Sum Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 132768/132768 K (Java/Others) Total ...
- Xor Sum 01字典树 hdu4825
Xor Sum Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 132768/132768 K (Java/Others)Total S ...
- hdu 4825 Xor Sum (01 Trie)
链接:http://acm.hdu.edu.cn/showproblem.php?pid=4825 题面: Xor Sum Time Limit: 2000/1000 MS (Java/Others) ...
- HDU--4825 Xor Sum (字典树)
题目链接:HDU--4825 Xor Sum mmp sb字典树因为数组开的不够大一直wa 不是报的 re!!! 找了一下午bug 草 把每个数转化成二进制存字典树里面 然后尽量取与x这个位置上不相同 ...
随机推荐
- ASP.NET Core 认证与授权[2]:Cookie认证 (笔记)
原文链接:https://www.cnblogs.com/RainingNight/p/cookie-authentication-in-asp-net-core.html 由于HTTP协议是无状态的 ...
- PAT——甲级1042:Shuffling Mashine
终于做到甲级了 就一个感觉....题目是真的看不懂,亏我还是四六级都过了的人....可是看完题愣是一点都不懂是什么意思. 1042 Shuffling Machine (20 point(s)) Sh ...
- [ecmagent][redis学习][1初识redis] python操作redis
#1 连接redis # 连接redis -- import redis -- 使用端口连接redis conn = redis.Redis(host=) -- 使用套接字连接 r = redis.R ...
- JavaSE复习(一)继承多态与常用API
继承与多态 在父子类的继承关系当中,如果成员变量重名,则创建子类对象时,访问有两种方式: 直接通过子类对象访问成员变量:等号左边是谁,就优先用谁,没有则向上找 间接通过成员方法访问成员变量:该方法属于 ...
- Java Web Action DAO Service层次理解
参考来源:http://blog.csdn.net/inter_peng/article/details/41021727 1. Action/Service/DAO简介: Action是管理业务(S ...
- VB.NET视频总结——基础篇
VB.NET视频是台湾讲师曹祖胜和林煌章共同带来的经典视频,视频中老师的台湾腔特别重,听起来有些别扭.而且对于计算机方面的术语翻译的与大陆有很大差异,所以刚开始看视频的时候总是进入不了状态,一头雾水的 ...
- PHP文件信息获取函数
知识点: basename():获取文件名,传入第二个参数则只显示文件名,不显示后缀 dirname():获取文件路径 pathinfo():将文件信息存入一个数组,通过索引basename,dirn ...
- 第十四篇:JavaScript
本篇内容 简介 使用 DOM 一. 简介 JavaScript 是世界上最流行的编程语言. 这门语言可用于 HTML 和 web,更可广泛用于服务器.PC.笔记本电脑.平板电脑和智能手机等设备. Ja ...
- [codeforces] 97B Superset || 平面分治
原题 给出一个平面的一些点,让你添加点,使得所有点对满足以下三个要求中的一个: 1.在一个水平面上 2.在一个竖直线上 3.以这两个点为对角的矩形内包含有其他点 输出一种可行解 因为只需要可行解,且只 ...
- rem布局和hotcss原理分析
rem布局的开源方案hotcss, 其原理个人理解如下: 手机px = (手机页面宽度/设计稿宽度) * 设计稿px 手机rem = 手机px / fontSize = (手机页面宽度/设计稿宽度) ...