Problem Codeforces Round #539 (Div. 2) - C. Sasha and a Bit of Relax
Time Limit: 2000 mSec
Problem Description
Input
The first line contains one integer n (2≤n≤3⋅10^5) — the size of the array.
The second line contains n integers a1,a2,…,an (0≤ai<2^20) — array itself.
Output
Print one integer — the number of funny pairs. You should consider only pairs where r−l+1is even number.
Sample Input
5
1 2 3 4 5
Sample Output
1
题解:这个题只要知道异或满足
if A ^ B == C then A == B ^ C 这个性质
就会很简单,首先处理出异或前缀和是很自然的,之后对于满足条件的pair,必定有f(r) == f(l - 1),f即异或前缀和,这是必要条件,同时也是充分的,充分性同样利用这个性质
若有a[l] ^ a[l+1] ^ ... ^ a[k] = a[k+1] ^ a[k + 2] ^ ... ^ a[r], 那么就可以利用上述性质使得k == mid,因为如果k > mid,那么,等式两边同时异或a[k]即可将a[k]从等式左边变到右边,继续进行这种操作知道k == mid,对于k < mid,同理,因此现在就是找满足异或前缀和相等的pair,并且要使得r - l + 1是偶数,也就是r 和 l - 1同奇偶,这个问题很好解决,统计每种异或前缀和的个数(对每个值按下标奇偶性分类)即可计算出最终结果,由a数组数据的范围可知异或前缀和 < 2^21,复杂度是完全可以接受的,别忘了开 long long。
#include <bits/stdc++.h> using namespace std; #define REP(i, n) for (int i = 1; i <= (n); i++)
#define sqr(x) ((x) * (x)) const int maxn = + ;
const int maxm = + ;
const int maxs = + ; typedef long long LL;
typedef pair<int, int> pii;
typedef pair<double, double> pdd; const LL unit = 1LL;
const int INF = 0x3f3f3f3f;
const double eps = 1e-;
const double inf = 1e15;
const double pi = acos(-1.0);
const int SIZE = + ;
const LL MOD = ; LL cnt[maxn][];
int n; int main()
{
ios::sync_with_stdio(false);
cin.tie();
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
cin >> n;
int pre = , x;
for (int i = ; i <= n; i++)
{
cin >> x;
pre ^= x;
//cout << pre << endl;
cnt[pre][i % ]++;
}
LL ans = ;
cnt[][]++;
for (int i = ; i < maxn; i++)
{
ans += cnt[i][] * (cnt[i][] - ) / ;
ans += cnt[i][] * (cnt[i][] - ) / ;
}
cout << ans << endl;
return ;
}