C. Sasha and a Bit of Relax
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
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 aa with nn integers. You need to count the number of funny pairs (l,r)(l,r) (l≤r)(l≤r). To check if a pair (l,r)(l,r) is a funny pair, take mid=l+r−12mid=l+r−12, then if r−l+1r−l+1 is an even number and al⊕al+1⊕…⊕amid=amid+1⊕amid+2⊕…⊕aral⊕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 ll to rr 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 nn (2≤n≤3⋅1052≤n≤3⋅105) — the size of the array.
The second line contains nn integers a1,a2,…,ana1,a2,…,an (0≤ai<2200≤ai<220) — array itself.
Output
Print one integer — the number of funny pairs. You should consider only pairs where r−l+1r−l+1 is even number.
Examples
input
Copy
5 1 2 3 4 5
output
Copy
1
input
Copy
6 3 2 2 3 7 6
output
Copy
3
input
Copy
3 42 4 2
output
Copy
0
Note
Be as cool as Sasha, upsolve problems!
In the first example, the only funny pair is (2,5)(2,5), as 2⊕3=4⊕5=12⊕3=4⊕5=1.
In the second example, funny pairs are (2,3)(2,3), (1,4)(1,4), and (3,6)(3,6).
In the third example, there are no funny pairs.
这一道题,有难度。。
先看一下异或的定义,和性质对这一题有帮助。
首先异或满足交换律 :a⊕b=b⊕a;
然后a⊕a=0,0⊕a=a;
这一题的要求的就是一个l,r;
满足左区间的数异或等于右区间的异或。
我们可以对异或进行前缀异或。(就是从第一个数异或到最后一个异或)
这样操作如果一个区间满足题意,那么这个区间内的数互相异或为0;
比如abcde,b⊕c=d⊕e;
那么b⊕c⊕d⊕e=0;
只要出现区间异或为0就说明有一个这样的数对。
我们前缀异或操作只要找到异或到当前位置的值前面出现过,那么,这之间的区间就是满足题意的(而且要满足奇偶性相同);
就像a⊕b⊕c⊕d⊕e=a,那我们就找到了一个数对。
如果出现2次就加2;
然后别忘了用longlong就行
自己想一下就明白了。
#include <cstdio>
#include <string>
#include<list>
#include<cstring>
#include<iostream>
#include<cmath>
typedef long long ll;
#define M (1<<20)+111
int a[2][M];
using namespace std;
int main()
{
ll n,x,ans=0,cnt=0;
cin>>n;
a[0][0]=1;
for(int i=1;i<=n;i++)
{
cin>>x;
ans^=x;
cnt+=a[i%2][ans];
a[i%2][ans]++;
//printf(" %d %d %d\n",i,ans,x );
}
cout<<cnt<<endl;
return 0;
}
人一我百,人百我万。夕林山寸,寻梦指尖!