一、内容
Catherine received an array of integers as a gift for March 8. Eventually she grew bored with it, and she started calculated various useless characteristics for it. She succeeded to do it for each one she came up with. But when she came up with another one — xor of all pairwise sums of elements in the array, she realized that she couldn't compute it for a very large array, thus she asked for your help. Can you do it? Formally, you need to compute(a1+a2)⊕(a1+a3)⊕…⊕(a1+an)⊕(a2+a3)⊕…⊕(a2+an)…⊕(an−1+an)Here x⊕yis a bitwise XOR operation (i.e. x ^ yin many modern programming languages). You can read about it in Wikipedia: https://en.wikipedia.org/wiki/Exclusive_or#Bitwise_operation.
Input
The first line contains a single integer n(2≤n≤400000) — the number of integers in the array.The second line contains integers a1,a2,…,an(1≤ai≤107 ).
Output
Print a single integer — xor of all pairwise sums of integers in the given array.
Examples
Input
2
1 2
Output
3
Input
3
1 2 3
Output
2
加粗样式
二、思路
三、代码
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 4e5 + 5;
int n, a[N], b[N], ans;
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
for (int i = 0; i <= 25; i++) { //枚举答案的每一位
int mod = 1 << (i + 1);
for (int j = 1; j <= n; j++) b[j] = a[j] % mod; //去掉i前面的位
sort(b + 1, b + 1 + n);
int cnt = 0;
for (int k = 1; k <= n; k++) {//固定a[k]
//[2^i - b[k], 2^i+1 - 1 - b[k]]
int l = lower_bound(b + 1, b + 1 + n, (1 << i) - b[k]) - b ;
int r = lower_bound(b + 1, b + 1 + n, (1 << (i+1)) - b[k]) - b - 1;
cnt += r - l + 1;
//[2^i+1 + 2 ^ i- b[k], 2^i+2 - 1 - b[k]]
l = lower_bound(b + 1, b + 1 + n, (1 << (i+1)) + (1 << i) - b[k]) - b;
cnt += n - l + 1;
if ((b[k] + b[k]) & (1 << i)) cnt--; //自己和自己要去掉
}
// /2 是因为我统计了2倍的个数 比如b1 + b2 在 b2 + b1的时候重复了
if ((cnt / 2) & 1) ans += 1 << i;
}
printf("%d\n", ans);
return 0;
}