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 <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
#include <queue>
#include <stack>
#include <cstdlib>
#include <iomanip>
#include <cmath>
#include <cassert>
#include <ctime>
#include <map>
#include <set>
using namespace std;
#pragma comment(linker, "/stck:1024000000,1024000000")
#define lowbit(x) (x&(-x))
#define max(x,y) (x>=y?x:y)
#define min(x,y) (x<=y?x:y)
#define MAX 100000000000000000
#define MOD 1000000007
#define pi acos(-1.0)
#define ei exp(1)
#define PI 3.1415926535897932384626433832
#define ios() ios::sync_with_stdio(true)
#define INF 0x3f3f3f3f
#define mem(a) (memset(a,0,sizeof(a)))
typedef long long ll;
int ans[],n,a[];
ll pos=,cnt=;
int main()
{
scanf("%d",&n);
int k=;
for(int i=;i<=n;i++)
{
scanf("%d",&a[i]);
int flag=;
for(int j=;j<=;j++)
{
if(a[i]&(<<j)) ans[j]++;
if(ans[j]>) {flag=;}
}
if(!flag)
{
pos+=cnt;
while(k<i)
{
int ok=;
for(int j=;j<=;j++)
{
ans[j]-=(a[k]&(<<j))?:;
if(ans[j]>) ok=;
}
k++;
cnt--;
if(ok) break;
else pos+=cnt;
}
}
cnt++;
}
printf("%lld\n",pos+((+cnt)*cnt/));
return ;
}
Xor Sum 2(位运算)的更多相关文章
- [CF703D]Mishka and Interesting sum/[BZOJ5476]位运算
[CF703D]Mishka and Interesting sum/[BZOJ5476]位运算 题目大意: 一个长度为\(n(n\le10^6)\)的序列\(A\).\(m(m\le10^6)\)次 ...
- Codeforces Round #716 (Div. 2), problem: (B) AND 0, Sum Big位运算思维
& -- 位运算之一,有0则0 原题链接 Problem - 1514B - Codeforces 题目 Example input 2 2 2 100000 20 output 4 2267 ...
- CodeForces 288C Polo the Penguin and XOR operation (位运算,异或)
题意:给一个数 n,让你求一个排列,使得这个排列与0-n的对应数的异或之最大. 析:既然是异或就得考虑异或的用法,然后想怎么才是最大呢,如果两个数二进制数正好互补,不就最大了么,比如,一个数是100, ...
- CF D. Ehab and the Expected XOR Problem 贪心+位运算
题中只有两个条件:任意区间异或值不等于0或m. 如果只考虑区间异或值不等于 0,则任意两个前缀异或值不能相等. 而除了不能相等之外,还需保证不能出现任意两个前缀异或值不等于m. 即 $xor[i]$^ ...
- Codeforces Global Round 8 D. AND, OR and square sum(位运算)
题目链接:https://codeforces.com/contest/1368/problem/D 题意 给出一个大小为 $n$ 的数组 $a$,每次可以选两个下标不同的元素,一个赋为二者相与的值, ...
- Codeforces Global Round 8 D. AND, OR and square sum (贪心,位运算)
题意:有\(n\)个数,选择某一对数使二者分别\(or\)和\(and\)得到两个新值,求操作后所有数平方和的最大值. 题解:不难发现每次操作后,两个数的二进制表示下的\(1\)的个数总是不变的,所以 ...
- Go位运算
目录 &(AND) |(OR) ^(XOR) &^(AND NOT) << 和 >> & 位运算 AND | 位运算 OR ^ 位运算 XOR & ...
- HDU 4825 Xor Sum 字典树+位运算
点击打开链接 Xor Sum Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 132768/132768 K (Java/Others) ...
- hdu 4825 xor sum(字典树+位运算)
Xor Sum Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 132768/132768 K (Java/Others)Total S ...
随机推荐
- node17
一.复习 cookie是在res中设置,req中读取的.第一次的访问没有cookie. cookie的存储大小有限,kv对.对用户可见,用户可以禁用.清除Cookie.可以被篡改. cookie用来制 ...
- webpack的像素转vw loader插件
这是一款针对webpack的像素转vw单位的loader插件. 笔者公司中,h5 rem的开发方案目前已经渐渐开始转向vw方案,因此本工具应运而生. 目前所制作的h5,大部分设计稿分辨率都是750的宽 ...
- Ubuntu系统下安装Eclipse
第一步:查看操作系统位数. 打开终端,输入file /sbin/init 可以看到笔者Ubuntu系统为32位,读者可以使用该命令获取自己机器上的操作系统位数. 这一步是最至关重要的一步,笔者机器处理 ...
- swift语言点评二十一-协议
定义有什么,及哪些必须实现. A protocol defines a blueprint of methods, properties, and other requirements that su ...
- AJAX和JSON笔记
### 1. 响应正文 传统的处理请求时,响应的方式有转发或重定向,无论是哪种,最终都会直接呈现某个页面给客户端,这样做的缺点在于: 1. 用户体验可能不好,例如:用户注册时,提交的用户名被占用, ...
- 贰、js的基础(三)数组
JS中数组的操作 1.数组的创建 var arrayObj = new Array(); //创建一个数组 var arrayObj = new Array([size]); //创建一个数组并指定长 ...
- phpstudy创建新站点-默认打不开
phpstudy中Apache中httpd.conf中如下语句前#要有 #Include conf/extra/httpd-vhosts.conf
- luogu P1592 互质(欧拉函数)
题意 (n<=106,k<=108) 题解 一开始以为是搜索. 但想想不对,翻了一眼题解发现是欧拉函数. 因为 gcd(a,b)=gcd(a,a+b) 所以和n互质的数应该是类似a1,a2 ...
- python 序列化和反序列化
概念 序列化: 将对象的状态信息转换为可以存储或传输的形式的过程.就是把对象转换成字符串的过程 反序列化: 把字符串转换成python可以识别的数据类型对象的过程 应用 #数据存储 #网络传输 模块 ...
- MySQL中锁详解(行锁、表锁、页锁、悲观锁、乐观锁等)
原文地址:http://blog.csdn.net/mysteryhaohao/article/details/51669741 锁,在现实生活中是为我们想要隐藏于外界所使用的一种工具.在计算机中,是 ...