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=l+r−12, then if r−l+1 is an even number and al⊕al+1⊕ …⊕amid=amid+1⊕amid+2⊕…⊕ar, 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⋅105) — the size of the array.
The second line contains n integers a1,a2,…,an (0≤ai<220) — 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
5
1 2 3 4 5
Output
1
Input
6
3 2 2 3 7 6
Output
3
Input
3
42 4 2
Output
0
给出一个数组,求在这个数组中有多少个符合要求的区间。要求如下:设区间为(l,r)
- l+r-1为偶数
- al⊕al+1⊕…⊕amid=amid+1⊕amid+2⊕…⊕ar
首先说明XOR运算
1 XOR 1=0 | 0 XOR 0=0
1 XOR 0=1 | 0 XOR 1=1
且XOR满足运算的
-
交换律
a ^ b = b ^ a
-
结合律
a ^ (b ^ c) = (a ^ b) ^ c
-
分配率
a ^ (b+c) = a ^ b + a ^ c
-
自反
a ^ b ^ b = a ^ 0 = a
利用自反的性质,可以实现一些小操作(与题目无关),如
交换两个数 : a=a ^ b; b=a ^ b; a=a ^ b;
判断两个数是否相等: a ^ b ==0
回到正题,记
XOR(al,ar)表示 al⊕al+1⊕…⊕amid⊕amid+1⊕amid+2⊕…⊕ar
由异或性质不难得出 A ^ B = C,A = B ^ C
故判断条件可进一步写为
0⊕al⊕al+1⊕…⊕amid=amid+1⊕amid+2⊕…⊕ar
0=al⊕al+1⊕…⊕amid⊕amid+1⊕amid+2⊕…⊕ar
0=XOR(al,ar)
这样得出一个结论,若在区间(l,r)上XOR(al,ar)=0,则对于任意的 i 属于区间(l,r) (包括区间端点) ,有XOR(al,ai)==XOR(ai+1,ar)
Ps:如有错误还请指正
记product[i] 表示 XOR(a0,ai),根据异或自反的性质,条件 0=XOR(al,ar) 可变为product[l-1] == product[r]。
由此得知,首先要预处理所给数组,得出product[]
此外,题目中另一个要求为 l+r-1 为偶数。所以l,r不能同奇偶,(l-1),r一定同奇偶。
所以,现在只需要找出有多少同奇偶位置上product[]相同的位置,即当 i%2==j%2 (i<j)时,满足条件product[i] == product[j]的数对(i,j)有多少对
#include <stdio.h>
#include <climits>
#include <cstring>
#include <time.h>
#include <math.h>
#include <iostream>
#include <algorithm>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <utility>
#include <vector>
#include <string>
#define INF 0x3f3f3f3f
#define ll long long
#define Pair pair<int,int>
#define re return
#define Make(a,b) make_pair(a,b)
#define Push(num) push_back(num)
#define rep(index,star,finish) for(register int index=star;index<finish;index++)
#define drep(index,finish,star) for(register int index=finish;index>=star;index--)
using namespace std;
const int maxn=1<<21;
int store[2][maxn];
int main(){
ios::sync_with_stdio(false);
int len;
cin>>len;
memset(store,0,sizeof(store));
store[0][0]++;
int temp,num=0;;
ll ans=0;
rep(i,1,len+1){
cin>>temp;
num=num ^ temp;
ans+=store[i%2][num];
store[i%2][num]++;
}
cout<<ans<<endl;
re 0;
}