HDU 3949 XOR

Problem Description
XOR is a kind of bit operator, we define that as follow: for two binary base number A and B, let C=A XOR B, then for each bit of C, we can get its value by check the digit of corresponding position in A and B. And for each digit, 1 XOR 1 = 0, 1 XOR 0 = 1, 0 XOR 1 = 1, 0 XOR 0 = 0. And we simply write this operator as ^, like 3 ^ 1 = 2,4 ^ 3 = 7. XOR is an amazing operator and this is a question about XOR. We can choose several numbers and do XOR operatorion to them one by one, then we get another number. For example, if we choose 2,3 and 4, we can get 2^3^4=5. Now, you are given N numbers, and you can choose some of them(even a single number) to do XOR on them, and you can get many different numbers. Now I want you tell me which number is the K-th smallest number among them.
 

Input
First line of the input is a single integer T(T<=30), indicates there are T test cases.
For each test case, the first line is an integer N(1<=N<=10000), the number of numbers below. The second line contains N integers (each number is between 1 and 10^18). The third line is a number Q(1<=Q<=10000), the number of queries. The fourth line contains Q numbers(each number is between 1 and 10^18) K1,K2,......KQ.
 

Output
For each test case,first output Case #C: in a single line,C means the number of the test case which is from 1 to T. Then for each query, you should output a single line contains the Ki-th smallest number in them, if there are less than Ki different numbers, output -1.
 

Sample Input
  
  
2 2 1 2 4 1 2 3 4 3 1 2 3 5 1 2 3 4 5
 

Sample Output
  
  
Case #1: 1 2 3 -1 Case #2: 0 1 2 3 -1
Hint
If you choose a single number, the result you get is the number you choose. Using long long instead of int because of the result may exceed 2^31-1.


题意就是给你n个数,q个询问每次询问这n个数可以异或出来的第k小的数


刚好把一直拖欠着的线性基给学了

大体就是每一个二进制位看成一个维度,所有的数就可以看成一个线性空间,然后对这个空间求出一组基【大概是这个意思?】

得到基以后再改造成每一维都互相无关的,也就是消成一个上三角矩阵

然后从大往小扫描这些基,按照类似快速幂的做法就可以求出哪些基在答案里

具体的线性基求法建议去搜一下,是用高斯消元的做法,应该讲的比我清楚多了

#include<map>
#include<queue>
#include<vector>
#include<cstdio>
#include<string>
#include<cstring>
#include<cstdint>
#include<iostream>
#include<algorithm>
using namespace std;
int n,cnt;
long long a[10001];
long long p[64],d[64];
inline void guass()
{
	memset(d,0,sizeof(d));
	int i,j;
	for(i=1;i<=n;i++)
	{
		for(j=63;j>=0;j--)
		{
			if((a[i]>>j)&1)
			{
				if(d[j])
					a[i]^=d[j];
				else
				{
					d[j]=a[i];
					break;
				}
			}
		}
	}
}
inline void rebuild()
{
	cnt=0;
	int i,j;
	for(i=63;i>=0;i--)
	{
		for(j=i-1;j>=0;j--)
		{
			if((d[i]>>j)&1)
				d[i]^=d[j];
		}
	}
	for(i=0;i<63;i++)
		if(d[i])
			p[cnt++]=d[i];
}
inline long long ask(long long k)
{
	if(n!=cnt)
		k--;
	if(k>=(1LL<<cnt))
		return -1;
	long long ret=0;
	int i;
	for(i=63;i>=0;i--)
	{
		if((k>>i)&1)
			ret^=p[i];
	}
	return ret;
}
int main()
{
	int T,kk=0;
	scanf("%d",&T);
	while(T>0)
	{
		T--;
		kk++;
		scanf("%d",&n);
		int i;
		for(i=1;i<=n;i++)
			scanf("%I64d",&a[i]);
		guass();
		rebuild();
		int q;
		scanf("%d",&q);
		printf("Case #%d:\n",kk);
		while(q>0)
		{
			q--;
			long long k;
			scanf("%I64d",&k);
			printf("%I64d\n",ask(k));
		}
	}
	return 0; 
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值