https://abc098.contest.atcoder.jp/tasks/arc098_b
从二进制的角度来看,若两个数的异或值等于两个数的和,则要求这两个数的任何位数都不能同时为1,即这两个数取与运算结果为0。 比如a = 3 = 011B, b = 4 = 100B,则有a & b = 0, a ^ b = a + b = 7 又如c = 5 = 101B, d = 7 = 111B,则有c & d != 0, c ^ d = 2, c + d = 12
#include <iostream>
using namespace std;
int a[200005];
long long sum;
int main()
{
int n;
cin >> n;
for(int i = 1; i <= n; i++)
{
cin >> a[i];
}
int left = 1;
int xorVal = 0; // 累计的异或结果
for(int right = 1; right <= n; right++)
{
while(xorVal & a[right])
{
xorVal ^= a[left];
left++;
}
xorVal ^= a[right];
sum += right - left + 1;
}
cout << sum;
return 0;
}