Xor Sum 2 (atcoder) (前缀异或和技巧)

There is an integer sequence A of length N.

Find the number of the pairs of integers l and r (1≤lrN) 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

The XOR of integers c1,c2,…,cm is defined as follows:

  • Let the XOR be X. In the binary representation of X, the digit in the 2k's place (0≤kk is an integer) is 1 if there are an odd number of integers among c1,c2,…cm whose binary representation has 1 in the 2k's place, and 0 if that number is even.

For example, let us compute the XOR of 3 and 5. The binary representation of 3 is 011, and the binary representation of 5 is 101, thus the XOR has the binary representation 110, that is, the XOR is 6.

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≤lrN) 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 Axor 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

首先我们要知道s[R]^s[L-1]=s[L]^s[L+1]………^s[R],这里用到了前缀和技巧,以及a^a=0.

#include<bits/stdc++.h>
using namespace std;
const int maxn=2e5+5;
long long f[maxn],s[maxn];
long long ans;
int main()
{
    int n,x;
    scanf("%d",&n);
    f[0]=0,s[0]=0;
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&x);
        f[i]=f[i-1]+x;
        s[i]=s[i-1]^x;
    }
    int L=1;
    ans=0;
    for(int R=1;R<=n;R++)
    {
        while((f[R]-f[L-1])!=(s[R]^s[L-1])) L++;
        ans+=R-L+1;
    }
    printf("%lld\n",ans);
}

 

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值