Keep On Going! |

HN-wrp

园龄:5年9个月粉丝:25关注:19

HDU 3949 XOR

线性基板子题,注意特判0,开long long就好。

#include <cstdio>
#include <cstring>
using namespace std;
#define R register
#define LL long long
#define int long long
const int MAXN=1e4+10;
const int MB=63;
int n,q,cnt,zero;
LL p[MAXN],a[MAXN];
inline void ins(LL x) {
	for(R int i=MB;i>=0;i--) {
		if(x&(1LL<<i)) {
			if(!p[i]) { p[i]=x; return ;}
			else x^=p[i];
		}
	}
	if(x==0) zero=1;
}
int d[MAXN],top;
inline void rebuild() {
	cnt=0;top=0;
	for(R int i=MB;i>=0;i--)
		for(R int j=i-1;j>=0;j--)
			if(p[i]&(1LL<<j)) p[i]^=p[j];
	for(R int i=0;i<=MB;i++) if(p[i]) d[cnt++]=p[i];
}
inline LL kth(int k) {
	if(k>=(1LL<<cnt)) return -1;
	LL ans=0;
	for(R int i=MB;i>=0;i--)
		if(k&(1LL<<i)) ans^=d[i];
	return ans; 
}
inline void solve(int test) {
	
	memset(p,0,sizeof(p));zero=0;
	scanf("%lld",&n);
	for(R int i=1;i<=n;i++) scanf("%lld",&a[i]);
	for(R int i=1;i<=n;i++) ins(a[i]);
	rebuild();
	scanf("%lld",&q);
	printf("Case #%lld:\n",test);
	while(q--) {
		LL tmp; scanf("%lld",&tmp);
		printf("%lld\n",tmp-zero?kth(tmp-zero):0LL);
	}
}
signed main() {
	int test; scanf("%lld",&test);
	for(R int i=1;i<=test;i++)  solve(i);
	return 0;
}

本文作者:HN-wrp

本文链接:https://www.cnblogs.com/clover4/p/12813014.html

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   HN-wrp  阅读(342)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示
评论
收藏
关注
推荐
深色
回顶
收起