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. datagrid 编辑

    spark:DataGrid编辑状态进入需要在选中一行高亮状态之后,再次点击该单元格,这样,相信非常符合开发人员的控制手法及大众使用习惯,这,是一点非常好的改良. 同时,它取缔了itemEditEnd ...

  2. Java_Chapter6_类与对象

    6.1 类与对象的概念 6.2 定义类 class Cylinder { double radius; int height; double pi; void setCylinder(double r ...

  3. 将CString写入到本地文件中

    void SocketU::WritePacket2File(CString packet_str)//packet_str为待写入的字符串{ CTime time = CTime::GetCurre ...

  4. menubar下面的选项不可以输入中文

    这是一个QT5的bug. 1.不用中文,使用英文: 2.先输入中文,然后在属性Action里面的text里改成中文.

  5. Cnblog页面美化小记

    Cnblog页面美化小记 这两天我在网上翻找了许许多多的资料,打开了不计其数的博客,对着\(js\).\(html\).\(css\)等文件删删改改,在浏览器和\(vscode\)间辗转腾挪...总算 ...

  6. IE开发人员工具手册

    The DOM Explorer tool (CTRL + 1) The The DOM Explorer tool shows the structure of your webpage as it ...

  7. 一图看懂join、left join、right join、fulljoin间的区别

    INNER JOIN 关键字在表中存在至少一个匹配时返回行. LEFT JOIN 关键字从左表(table1)返回所有的行,即使右表(table2)中没有匹配.如果右表中没有匹配,则结果为 NULL. ...

  8. mysql 一些属性

    1)定义id,设置int,涉及的属性有: BINARY二进制 UNSIGNED无符号数 UNSIGNED ZEROFILL 在列字段中使用UNSIGNED ZEROFILL属性,如: 插入int(4) ...

  9. springboot处理session生命周期

    在使用springboot开发过程中发现用户登陆后60s后session就自动失效了,需要重新登陆,明明 application.yml  文件里已经配置了 server.session.timeou ...

  10. TextView两种显示link的方法

    TextView两种显示link的方法 一.简介 也是TextView显示文本控件两种方法 也是显示丰富的文本 二.方法 TextView两种显示link的方法  1)通过TextView里面的类ht ...