D. Present
time limit per test
3 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output
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)(a1+a2)⊕(a1+a3)⊕…⊕(a1+an)⊕(a2+a3)⊕…⊕(a2+an)…⊕(an−1+an)
Here x⊕yx⊕y is a bitwise XOR operation (i.e. xx ^ yy in 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 nn (2≤n≤4000002≤n≤400000) — the number of integers in the array.
The second line contains integers a1,a2,…,ana1,a2,…,an (1≤ai≤1071≤ai≤107).
Output
Print a single integer — xor of all pairwise sums of integers in the given array.
Examples
input
Copy
2 1 2
output
Copy
3
input
Copy
3 1 2 3
output
Copy
2
Note
In the first sample case there is only one sum 1+2=31+2=3.
In the second sample case there are three sums: 1+2=31+2=3, 1+3=41+3=4, 2+3=52+3=5. In binary they are represented as 0112⊕1002⊕1012=01020112⊕1002⊕1012=0102, thus the answer is 2.
⊕⊕ is the bitwise xor operation. To define x⊕yx⊕y, consider binary representations of integers xx and yy. We put the ii-th bit of the result to be 1 when exactly one of the ii-th bits of xx and yy is 1. Otherwise, the ii-th bit of the result is put to be 0. For example, 01012⊕00112=0110201012⊕00112=01102.
題意:給你n(<=4e5),再給你n個數a[1]~a[n](a[i]<=1e7),求
(a[1]+a[2])⊕(a[1]+a[3])⊕…⊕(a[1]+a[n])⊕(a[2]+a[3])⊕…⊕(a[2]+a[n])…⊕(a[n−1]+a[n])
其中⊕是異或運算。
思路:這題的思維難度還是很高的。考慮計算每一位的貢獻,考慮數據範圍只有1e7,最高25位。
假設我們計算第i位的貢獻,那麼數組a中高於i的位我們就可以不考慮。用另一個數組b,存下a%(2^(i+1)),然後對b進行排序。
從小到大枚舉b[j],這時候我們要找一些數x,加上b[j]之後第i位爲1。於是出現了以下兩種情況:
① (1<<i)-b[j]<=x<(1<<(i+1))-b[j]
①(1<<(i+1))+ (1<<i)-b[j]<=x<(1<<(i+2))-b[j]
顯然這兩種情況不交叉,因此分別用lower_bound計算即可。
(PS:高達18個出題人出的這場比賽的題,但是並不怎麼樣。題目難度順序不對,難度落差還這麼大,真是太讓我失望了)
代碼:
#include<bits/stdc++.h>
#define ll long long
#define inf 0x3f3f3f3f
#define rep(i,a,b) for(register int i=(a);i<=(b);i++)
#define dep(i,a,b) for(register int i=(a);i>=(b);i--)
using namespace std;
const int maxn=4e5+5;
//const double pi=acos(-1.0);
//const double eps=1e-9;
//const ll mo=1e9+7;
int n,m,k;
int a[maxn],b[maxn];
int main(){
scanf("%d",&n);
rep(i,1,n) scanf("%d",&a[i]);
int ans=0;
dep(i,25,0){
rep(j,1,n) b[j]=a[j]%(1<<(i+1));
sort(b+1,b+1+n);
rep(j,1,n){
int l=(1<<i)-b[j];
int r=(1<<(i+1))-b[j];
int k=lower_bound(b+j+1,b+n+1,r)-lower_bound(b+j+1,b+n+1,l);
if(k&1) ans^=(1<<i);
l=(1<<(i+1))+(1<<i)-b[j];
r=(1<<(i+2))-b[j];
k=lower_bound(b+j+1,b+n+1,r)-lower_bound(b+j+1,b+n+1,l);
if(k&1) ans^=(1<<i);
}
}
printf("%d\n",ans);
return 0;
}