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.

InputFirst 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.OutputFor 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

题意:给定N个数,所有集合的不同异或和中,求从小到大第K个,不存在则输出-1。

思路:我们知道线性基可以表示用不超过64个数,表示出所有集合的异或和,那么为0的部位不考虑,我们求第K个,就是等效表示成二进制。。。ok了。

先求线性基,得到p数组。然后把为0的忽略,并且前面的p对后面的效果求出来。 有个注意的问题就是0,因为线性基我们没有考虑0,所以0单独考虑,如果线性基的大小和原数组大小一样,则可以表示出来,那么K--;

#include<bits/stdc++.h>
#define ll long long
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define rep2(i,a,b) for(int i=a;i>=b;i--)
using namespace std;
const int maxn=;
ll p[],x;
int main()
{
int T,N,Q,Cas=;
scanf("%d",&T);
while(T--){
scanf("%d",&N);
rep(i,,) p[i]=;
rep(i,,N) {
scanf("%lld",&x);
rep2(j,,){
if(x&(1LL<<j)){
if(p[j]) x^=p[j];
else { p[j]=x;break;}
}
}
}
ll num=,ans,K;
rep(i,,) if(p[i]){
p[num++]=p[i];
rep(j,i+,) if((p[j]>>i)&) p[j]^=p[i];
}
scanf("%d",&Q);
printf("Case #%d:\n",++Cas);
while(Q--){
scanf("%lld",&K); if(N!=num) K--; //here,notice!考虑0的存在性
if(K>=(1LL<<num)) puts("-1");
else {
ans=;
rep(j,,) {
if(K&(1LL<<j)) ans^=p[j]; //不能加,还是用异或,可能有尾巴,相互抵消
}
printf("%I64d\n",ans);
}
}
}
return ;
}

HDU - 3949 :XOR(线性基,所有集合的不同异或和中,求从小到大第K个)的更多相关文章

  1. HDU 3949 XOR [线性基|高斯消元]

    目录 题目链接 题解 代码 题目链接 HDU 3949 XOR 题解 hdu3949XOR 搞死消元找到一组线性无关组 消出对角矩阵后 对于k二进制拆分 对于每列只有有一个1的,显然可以用k的二进制数 ...

  2. hdu 3949 XOR (线性基)

    链接: http://acm.hdu.edu.cn/showproblem.php?pid=3949 题意: 给出n个数,从中任意取几个数字异或,求第k小的异或和 思路: 线性基求第k小异或和,因为题 ...

  3. HDU 3949 XOR 线性基

    http://acm.hdu.edu.cn/showproblem.php?pid=3949 求异或第k小,结论是第k小就是 k二进制的第i位为1就把i位的线性基异或上去. 但是这道题和上一道线性基不 ...

  4. hdu 3949 XOR 线性基 第k小异或和

    题目链接 题意 给定\(n\)个数,对其每一个子集计算异或和,求第\(k\)小的异或和. 思路 先求得线性基. 同上题,转化为求其线性基的子集的第k小异或和. 结论 记\(n\)个数的线性基为向量组\ ...

  5. HDU 3949 XOR ——线形基 高斯消元

    [题目分析] 异或空间的K小值. 高斯消元和动态维护线形基两种方法都试了试. 动态维护更好些,也更快(QAQ,我要高斯消元有何用) 高斯消元可以用来开拓视野. 注意0和-1的情况 [代码] 高斯消元 ...

  6. HDU 3949 XOR(高斯消元搞基)

    HDU 3949 XOR pid=3949" target="_blank" style="">题目链接 题意:给定一些数字,问任取几个异或值第 ...

  7. HDU 3949 XOR [高斯消元XOR 线性基]

    3949冰上走 题意: 给你 N个数,从中取出若干个进行异或运算 , 求最后所有可以得到的异或结果中的第k小值 N个数高斯消元求出线性基后,设秩为$r$,那么总共可以组成$2^r$中数字(本题不能不选 ...

  8. HDU 3949 XOR (线性基第k小)题解

    题意: 给出\(n\)个数,求出子集异或第\(k\)小的值,不存在输出-1. 思路: 先用线性基存所有的子集,然后对线性基每一位进行消元,保证只有\(d[i]\)的\(i\)位存在1,那么这样变成了一 ...

  9. ACM学习历程—HDU 3949 XOR(xor高斯消元)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3949 题目大意是给n个数,然后随便取几个数求xor和,求第k小的.(重复不计算) 首先想把所有xor的 ...

随机推荐

  1. 一个用于实现并行执行的 Java actor 库

    即使 Java 6 和 Java 7 中引入并发性更新,Java 语言仍然无法让并行编程变得特别容易.Java 线程.synchronized 代码块.wait/notify 和java.util.c ...

  2. iOS 多线程安全 与 可变字典

    这周最大的收获是稍稍通透了 多线程安全字典的重要性.  诱因是,发现了有字典坏地址错误      果断以为是 value 或者 key 是可能出现了空值,补充了潜在的判断,虽然有的位置已经预判断的,但 ...

  3. web标准的理解

    首先,什么是web标准?web标准是w3c组织为解决跨浏览器兼容问题而推出的关于网页开发时应遵守的规范.在网页的四个部分中网页的内容是由网页开发者自己定义的,因此这一部分无法标准化,而网页的结构(HT ...

  4. Storm--命令行解析

    strom的命令行解析 Commands: activate classpath deactivate dev-zookeeper drpc help jar kill list localconfv ...

  5. 集合遍历(Set,List,Map)

    一.Set遍历 定义一个set Set<String> books = new HashSet<String>(); books.add("Java"); ...

  6. Zabbix JVM 安装

    Zabbix 服务端安装插件 系统:centos 7.4 x64 环境:zabbix 3.0.16 yum源:rpm -ivh http://repo.zabbix.com/zabbix/3.0/rh ...

  7. 常用iptables命令

    1.不让某个ip访问本机的80端口: iptables -t filter -I INPUT -p tcp -s 192.168.222.163 --dport 80 -j DROP 2.实现把访问 ...

  8. Android震动vibrator(马达)--系统到驱动的流程【转】

    本文转载自:https://blog.csdn.net/tianshiyalin/article/details/17136723 一.前言 本人刚学习安卓驱动开发,水平不能说菜,是根本没有水平,在这 ...

  9. 使用John the ripper工具来尝试破解Linux密码

    这篇文章主要介绍了使用John the ripper工具来尝试破解Linux密码的方法,这款工具可能主要被用来破解系统用户的密码以获得文件操作权限,需要的朋友可以参考下 John有别于Hdra之类的工 ...

  10. 编写一个程序,将 a.txt 文件中的单词与 b.txt 文件中的单词交替合并到 c.txt 文件中,a.txt 文件中的单词用回车符分隔,b.txt 文件中用回车或空格进行分隔。

    package IO; import java.io.*; public class test { public void connectWords(File file1, File file2, F ...