您的位置:首页 > 其它

hdu 5270 ZYB loves Xor II

2015-06-30 05:06 218 查看
题目链接

此类数论题大多按位考虑,显然暴力会超时。

不妨对(a+b)的第i位做考虑,将a,b都模2^(i+1),显然在(a+b)∈[2^i,2^(i+1))∪[3*2^i,2^(i+2))时该位为1.

所以我们只要求大于某个数的(a+b)的个数即可

值得注意的是,为了在排序时降低复杂度,可以让mod值从大到小然后归并排序,具体见代码。

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;

typedef unsigned long long ll;
const int maxn=(int)1e5+5;
int n;
ll a[maxn],b[maxn],c[maxn];

void merge (ll *p,ll mod) {   //在给定的mod下归并排序
int i,j,k,m;
for(i=1;i<=n;i++) {
c[i]=p[i];
if(p[i]>=mod)
break;
}
m=j=i;
for(i=j;i<=n;i++)
c[i]=p[i]%mod;
i=1;c[n+1]=mod<<1;
for(k=1;k<=n;k++) {
if(c[i]<c[j]&&i<m) p[k]=c[i++];
else p[k]=c[j++];
}
}

ll bigthan (ll m) {             //大于m的数对对数
int i,j=n;ll ans=0;
for (i=1;i<=n;i++) {
while ((a[i]+b[j])>=m&&j>0) j--;
ans+=n-j;
}
return ans;
}

bool get (ll mod) {               //考虑mod位是否为1
merge (a,mod<<1);
merge (b,mod<<1);
ll ans = bigthan(mod) + bigthan(3*mod) - bigthan(mod<<1);
if ( ans%2 ) return true;
return false;
}

int main ()
{
//	freopen("aa.txt","r",stdin);
int cas,T,i;ll mod,ans;
scanf("%d",&T);
for(cas=1;cas<=T;cas++) {
scanf("%d",&n);
for(i=1;i<=n;i++) scanf("%I64d",a+i);
for(i=1;i<=n;i++) scanf("%I64d",b+i);
sort(a+1,a+n+1);sort(b+1,b+n+1);
ans=0;mod=(ll)1<<61;
while(mod) {
if (get(mod))
ans+=mod;
mod=mod>>1;
}
cout<<"Case #"<<cas<<": "<<ans<<endl;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: