#include <iostream>
using namespace std;
const int N = 1E5 + 5;
int a[N];
int bit[N][21], cnt[2][21];
long long power(long long a, int b) {
long long ret = 1;
for (; b; b /= 2, a = a * a) {
if (b % 2) {
ret *= a;
}
}
return ret;
}
void add(int x, int i) {
for (int j = 0; j <= 20; j++) {
bit[i][j] = bit[i - 1][j] + ((x >> j) & 1);
cnt[bit[i][j] % 2][j]++;
}
}
long long cal(int x, int i) {
long long ret = 0;
for (int j = 0; j <= 20; j++) {
if ((x >> j) & 1) {
ret += power(2, j) * cnt[bit[i][j] % 2][j];
} else {
ret += power(2, j) * cnt[1 - (bit[i][j] % 2)][j];
}
cnt[bit[i][j] % 2][j]--;
}
return ret;
}
int main() {
int n;
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
add(a[i], i);
}
long long ans = 0;
for (int i = 1; i <= n; i++) {
ans += cal(a[i], i);
}
cout << ans;
return 0;
}
/*
数组元素按位表示
00100
01000
11000
00100
按位前缀和
00111
01111
12222
00111
3 + 3 + 3
2 + 2 * 4
4 * 4 + 4 * 4
8 * 3 + 8 * 3 + 8 * 3
9
10
32
72
*/
第十四届蓝桥杯省赛H题异或和之和
于 2023-04-12 07:55:56 首次发布