hdu3949:XOR

传送门:http://acm.hdu.edu.cn/showproblem.php?pid=3949

一个讲的比较好的博客:http://m.blog.csdn.net/blog/wdcjdtc/38300217

Orz hzwer:http://hzwer.com/5491.html


思路:为什么我搜线性基什么也没搜到呢..

用高斯消元维护线性基,然后....就是代码了

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
const int maxn=10010;
using namespace std;
typedef long long ll;
int T,n,m,tot;ll a[maxn],bin[65],x;char ch;bool zero;

void read(ll &x){
	for (ch=getchar();!isdigit(ch);ch=getchar());
	for (x=0;isdigit(ch);ch=getchar()) x=x*10+ch-'0';
}

void gauss(){
	tot=zero=0;
	for (int i=60;i>=0;i--){
		int j=tot+1;
		for (;!(bin[i]&a[j])&&j<=n;j++);
		if (j>n) continue;
		swap(a[++tot],a[j]);
		for (int j=1;j<=n;j++)
			if (j!=tot&&(a[j]&bin[i]))
				a[j]^=a[tot];
	}
	if (tot!=n) zero=1;
}

ll query(ll x){
	ll res=0;x-=zero;
	if (!x) return 0;
	if (x>=bin[tot]) return -1;
	for (int i=1;i<=tot;i++)
		if (x&bin[tot-i]) res^=a[i];
	return res;
}

int main(){
	bin[0]=1;for (int i=1;i<=62;i++) bin[i]=bin[i-1]<<1;
	scanf("%d",&T);
	for (int i=1;i<=T;i++){
		printf("Case #%d:
",i);
		memset(a,0,sizeof(a)),scanf("%d",&n);
		for (int j=1;j<=n;j++) read(a[j]);
		gauss(),scanf("%d",&m);
		for (int j=1;j<=m;j++)
			read(x),printf("%lld
",query(x));
	}
	return 0;
}


原文地址:https://www.cnblogs.com/thythy/p/5493555.html