#include <bits/stdc++.h>
#define int long long
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
using namespace std;
const int N = 1e6 + 11;
int a[N], b0[N], b0n, b1[N], b1n;
int d[N], d1[N], d2[N];
int32_t main(){
cin.tie(0); cout.tie(0)->sync_with_stdio(false);
int n; cin >> n;
for(int i = 0; i < n; i++){
cin >> a[i];
}
int ans = 0;
for(int s = 29; s >= 19; s--){
int c = 0;
b0n = b1n = 0;
for(int j = 0; j < n; j++){
if(a[j] & (1 << s)) b1[b1n++] = a[j] % (1 << s);
else b0[b0n++] = a[j] % (1 << s);
}
sort(b0, b0 + b0n); sort(b1, b1 + b1n);
int r = b0n;
for(int l = 0; l < b0n; l++){
r = max(r, l + 1);
while(r - 1 > l && b0[l] + b0[r - 1] >= (1 << s)) r--;
c += b0n - r;
}
r = b1n;
for(int l = 0; l < b1n; l++){
r = max(r, l + 1);
while(r - 1 > l && b1[l] + b1[r - 1] >= (1 << s)) r--;
c += b1n - r;
}
r = b1n;
for(int l = 0; l < b0n; l++){
while(r > 0 && b0[l] + b1[r - 1] >= (1 << s)) r--;
c += b1n - r;
}
c += b0n * b1n;
ans ^= (1 << s) * (c % 2);
}
for(int j = 0; j < n; j++) d[a[j] % (1 << 19)]++;
for(int s = 18; s >= 0; s--){
int c = 0;
b0n = accumulate(d, d + (1 << s), 0LL);
b1n = accumulate(d + (1 << s), d + (1 << s + 1), 0LL);
int tot = 0;
for(int i = 1; i < (1 << s) / 2; i++){
tot += d[(1 << s) - i];
c += tot * d[i];
}
tot = 0;
if(s != 0){
for(int i = (1 << s) / 2; i < (1 << s); i++){
tot += d[i];
}
c += tot * (tot - 1) / 2;
}
tot = 0;
for(int i = 1; i < (1 << s) / 2; i++){
tot += d[(1 << s) + (1 << s) - i];
c += tot * d[(1 << s) + i];
}
tot = 0;
if(s != 0){
for(int i = (1 << s) / 2; i < (1 << s); i++){
tot += d[(1 << s) + i];
}
c += tot * (tot - 1) / 2;
}
tot = 0;
for(int i = 1; i < (1 << s); i++){
tot += d[(1 << s) + (1 << s) - i];
c += tot * d[i];
}
c += b0n * b1n;
if(s){
for(int i = 0; i < (1 << s); i++) d[i] += d[i + (1 << s)];
}
ans ^= (1 << s) * (c % 2);
}
for(int i = 0; i < n; i++){
ans ^= (a[i] * 2);
}
cout << ans << endl;
}
Compilation message
xorsum.cpp: In function 'int32_t main()':
xorsum.cpp:53:46: warning: suggest parentheses around '+' inside '<<' [-Wparentheses]
53 | b1n = accumulate(d + (1 << s), d + (1 << s + 1), 0LL);
| ~~^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
4436 KB |
Output is correct |
2 |
Correct |
6 ms |
4436 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
750 ms |
17912 KB |
Output is correct |
2 |
Correct |
682 ms |
21196 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
750 ms |
17912 KB |
Output is correct |
2 |
Correct |
682 ms |
21196 KB |
Output is correct |
3 |
Correct |
1016 ms |
30088 KB |
Output is correct |
4 |
Correct |
990 ms |
29592 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
4436 KB |
Output is correct |
2 |
Correct |
6 ms |
4436 KB |
Output is correct |
3 |
Correct |
102 ms |
7116 KB |
Output is correct |
4 |
Correct |
103 ms |
7148 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
4436 KB |
Output is correct |
2 |
Correct |
6 ms |
4436 KB |
Output is correct |
3 |
Correct |
750 ms |
17912 KB |
Output is correct |
4 |
Correct |
682 ms |
21196 KB |
Output is correct |
5 |
Correct |
1016 ms |
30088 KB |
Output is correct |
6 |
Correct |
990 ms |
29592 KB |
Output is correct |
7 |
Correct |
102 ms |
7116 KB |
Output is correct |
8 |
Correct |
103 ms |
7148 KB |
Output is correct |
9 |
Correct |
1095 ms |
33408 KB |
Output is correct |
10 |
Correct |
1086 ms |
33440 KB |
Output is correct |
11 |
Correct |
1090 ms |
33296 KB |
Output is correct |