admin管理员组文章数量:1534187
Codeforces Round #626 (Div. 2, based on Moscow Open Olympiad in Informatics) D.Present
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⊕y is a bitwise XOR operation (i.e. x ^ y in many modern programming languages).
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
借鉴了大神的想法,对答案的每一位二分即可,AC代码如下:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=4e5+5;
int a[N],b[N],n,m,k,ans=0;
int main(){
cin>>n;
for(int i=1;i<=n;i++){
cin>>a[i];
}
for(int i=0;i<26;i++){
int mod=1<<(i+1);
for(int j=1;j<=n;j++)
b[j]=a[j]%mod;
sort(b+1,b+1+n);
int s=0;
for(int j=1;j<=n;j++){
int l,r;
l=lower_bound(b+1,b+1+n,(1<<i)-b[j])-b;
r=lower_bound(b+1,b+1+n,(1<<(i+1))-b[j])-b-1;
s+=r-l+1;
l=lower_bound(b+1,b+1+n,(1<<i)+(1<<(i+1))-b[j])-b;
r=lower_bound(b+1,b+1+n,(1<<(i+2))-b[j])-b-1;
s+=r-l+1;
if((b[j]+b[j])&(1<<i)) s--;
}
if((s/2)&1) ans+=1<<i;
}
cout<<ans;
return 0;
}
本文标签: DivBasedcodeforcesMoscowInformatics
版权声明:本文标题:Codeforces Round #626 (Div. 2, based on Moscow Open Olympiad in Informatics) D.Present 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://m.elefans.com/dongtai/1726873209a1088064.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论