裸题。线性基消成对角后, 最高位为 i 的数是唯一的。这个性质很好,使得选的数集中最大数的最高位,在异或后一定是 1。设
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long LL;
int _test,n,Q;
LL b[70],a[70];
void Insert(LL x){
for(int j=60;j>=0;j--) if((x>>j)&1){
if(b[j]) x^=b[j]; else{
b[j]=x; for(int k=j-1;k>=0;k--) if((b[j]>>k)&1) b[j]^=b[k];
for(int k=j+1;k<=60;k++) if((b[k]>>j)&1) b[k]^=b[j];
break;
}
}
}
int main(){
freopen("hdu3949.in","r",stdin);
freopen("hdu3949.out","w",stdout);
scanf("%d",&_test);
for(int ii=1;ii<=_test;ii++){
scanf("%d",&n);
memset(b,0,sizeof(b));
for(int i=1;i<=n;i++){
LL x; scanf("%lld",&x);
Insert(x);
}
memset(a,0,sizeof(a));
for(int i=0;i<=60;i++) if(b[i]) a[++a[0]]=b[i];
scanf("%d",&Q); printf("Case #%d:\n",ii);
while(Q--){
LL x; scanf("%lld",&x); x-=(a[0]<n);
LL ans=0,now=0;
do ans^=(a[++now]*(x&1)), x>>=1; while(x);
if(now>a[0]) printf("-1\n"); else printf("%lld\n",ans);
}
}
return 0;
}

本文介绍了一种解决特定类型问题的方法:通过线性基消元实现对整数集进行异或运算操作,并找到该集合中能使得结果最大的元素。文章提供了一个C++程序实例,演示了如何使用线性基技巧来解决此类问题。
472

被折叠的 条评论
为什么被折叠?



