Codeforces 1113C: Sasha and a Bit of Relax(位运算)

Sasha likes programming. Once, during a very long contest, Sasha decided that he was a bit tired and needed to relax. So he did. But since Sasha isn’t an ordinary guy, he prefers to relax unusually. During leisure time Sasha likes to upsolve unsolved problems because upsolving is very useful.

Therefore, Sasha decided to upsolve the following problem:

You have an array $a$ with $n$ integers. You need to count the number of funny pairs $(l,r) (l≤r)$. To check if a pair $(l,r)$ is a funny pair, take $mid=\frac{l+r-1}{2}$, then if $r−l+1$ is an even number and $a_{l} \oplus a_{l+1} \oplus \ldots \oplus a_{m i d}=a_{m i d+1} \oplus a_{m i d+2} \oplus \ldots \oplus a_{r}$, then the pair is funny. In other words, $⊕$ of elements of the left half of the subarray from $l$ to $r$ should be equal to $⊕$ of elements of the right half. Note that $⊕$ denotes the bitwise XOR operation.

It is time to continue solving the contest, so Sasha asked you to solve this task.

Input

The first line contains one integer $n (2≤n≤3⋅10^5)$ — the size of the array.

The second line contains $n$ integers $a_{1}, a_{2}, \dots, a_{n}\left(0 \leq a_{i}<2^{20}\right)$ — array itself.

Output

Print one integer — the number of funny pairs. You should consider only pairs where $r−l+1$ is even number.

Examples

input

1
2
5
1 2 3 4 5

output

1
1

input

1
2
6
3 2 2 3 7 6

output

1
3

input

1
2
3
42 4 2

output

1
0

Note

Be as cool as Sasha, upsolve problems!

In the first example, the only funny pair is $(2,5)$, as $2 \oplus 3=4 \oplus 5=1$.

In the second example, funny pairs are $(2,3)$, $(1,4)$, and $(3,6)$.

In the third example, there are no funny pairs.

题意

有$n$个数,对于偶数长度的区间$[l,r]$,$mid=\frac{l+r-1}{2}$,要求$[l,mid],[mid+1,r]$两个区间内的数的异或值相等,问有多少个这样的区间

Solve

利用异或的性质:出现偶数次的数异或值为$0$

如果$[l,mid],[mid+1,r]$区间数的异或值相等,则$[l,r]$区间的数的异或值为$0$

可以推出:如果当前位置的异或值出现过,并且之前出现的位置与当前出现位置下标的奇偶性相同,那么这两个位置之间的区域就是题目中要求的funny pairs

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
/*************************************************************************
> File Name: C.cpp
> Author: WZY
> Created Time: 2019年02月17日 19:59:36
************************************************************************/

#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define ms(a,b) memset(a,b,sizeof(a))
#define pi acos(-1.0)
#define INF 0x7f7f7f7f
const double E=exp(1);
const int maxn=1e6+10;
const int mod=1e9+7;
using namespace std;
ll sum[1<<20][2];
int main(int argc, char const *argv[])
{
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin>>n;
ll ans=0;
ll x;
ll res=0;
sum[0][0]=1;
for(int i=1;i<=n;i++)
{
cin>>x;
res^=x;
ans+=sum[res][i&1];
sum[res][i&1]++;
}
cout<<ans<<endl;
return 0;
}

本文标题:Codeforces 1113C: Sasha and a Bit of Relax(位运算)

文章作者:执念

发布时间:2019年02月17日 - 22:02

最后更新:2019年02月17日 - 22:02

原始链接:https://blog.wzy1999.wang/solve/cf1113C/

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。

-------------本文结束感谢您的阅读-------------
0%