Xor Sum 2(位运算)
Posted 十年换你一句好久不见
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 XORConstraints
- 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[26],n,a[200006]; ll pos=0,cnt=0; int main() { scanf("%d",&n); int k=1; for(int i=1;i<=n;i++) { scanf("%d",&a[i]); int flag=1; for(int j=0;j<=20;j++) { if(a[i]&(1<<j)) ans[j]++; if(ans[j]>1) {flag=0;} } if(!flag) { pos+=cnt; while(k<i) { int ok=1; for(int j=0;j<=20;j++) { ans[j]-=(a[k]&(1<<j))?1:0; if(ans[j]>1) ok=0; } k++; cnt--; if(ok) break; else pos+=cnt; } } cnt++; } printf("%lld\n",pos+((1+cnt)*cnt/2)); return 0; }
以上是关于Xor Sum 2(位运算)的主要内容,如果未能解决你的问题,请参考以下文章