Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 3525    Accepted Submission(s): 1212

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.

 
很模板的线性基求第k小。
只需要记录线性基中元素的个数,插入完了再改造一下线性基使得任意两位不相关。
如果线性基的元素个数<总的数的个数那么就可以异或出0.
 
code:
#include<iostream>
#include<cstdio>
#include<cstring>
#define ll long long
using namespace std;
ll ci[],n,T,now,q;
struct xxj{
ll a[],siz;
ll p[],len; void clear(){
memset(a,,sizeof(a));
memset(p,,sizeof(p));
len=siz=;
} void ins(ll x){
for(int i=;i>=;i--) if(x&ci[i]){
if(!a[i]){
a[i]=x;
siz++;
break;
}
x^=a[i];
}
} void update(){
for(int i=;i>=;i--) if(a[i])
for(int j=i-;j>=;j--) if(a[i]&ci[j]) a[i]^=a[j];
for(int i=;i<=;i++) if(a[i]) p[len++]=a[i];
} ll kth(ll k){
if(k>=ci[len]) return -;
ll ans=;
for(int i=;i<len;i++) if(k&ci[i]) ans^=p[i];
return ans;
} }mine; int main(){
ci[]=;
for(int i=;i<=;i++) ci[i]=ci[i-]+ci[i-]; scanf("%lld",&T);
for(int l=;l<=T;l++){
printf("Case #%d:\n",l); mine.clear();
scanf("%lld",&n);
for(int i=;i<=n;i++){
scanf("%lld",&now);
mine.ins(now);
} mine.update(); scanf("%lld",&q);
while(q--){
scanf("%lld",&now);
if(mine.siz<n) now--;
printf("%lld\n",mine.kth(now));
}
} return ;
}

HDOJ(HDU) 3949 XOR的更多相关文章

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

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

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

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

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

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

  4. HDU 3949 XOR(高斯消元)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3949 题意:给出一个长度为n的数列A.选出A的所有子集(除空集外)进行抑或得到2^n-1个数字,去重排 ...

  5. hdu 3949 XOR (线性基)

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

  6. HDU 3949 XOR 线性基

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

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

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

  8. HDU 3949 XOR

    3949 思路: 线性基,线性基的每个元素尽可能小 将k转换成二进制与排好序的线性基相对应 如果线性基的个数小于n,说明n个元素线性相关,所以可以构成0,k要减1 代码: #pragma GCC op ...

  9. HDU 3949 XOR 高斯消元

    题目大意:给定一个数组,求这些数组通过异或能得到的数中的第k小是多少 首先高斯消元求出线性基,然后将k依照二进制拆分就可以 注意当高斯消元结束后若末尾有0则第1小是0 特判一下然后k-- 然后HDU输 ...

随机推荐

  1. 20155335俞昆《java程序设计》第三周总结

    20155335  2006-2007-2  <Java程序设计>第三周学习总结 ##  教材学习内容总结 首先,关键是区基本类型和类类型,,产生对象必须定义类,类是一个概念,并不存在,对 ...

  2. HDU 2577 How to Type (字符串处理)

    题目链接 Problem Description Pirates have finished developing the typing software. He called Cathy to te ...

  3. CCC2018游记

    day (-1) 晚上睡觉没盖被子 day 0  2018年2月13日 下午放学回来感到一阵头痛,一量体温结果发烧了,感觉很蓝瘦,居然在CCC前一天生病. 本来注册了账号想打practise的,结果又 ...

  4. 14、char和varchar的区别?

    就长度来说: ♣ char的长度是不可变的; ♣ 而varchar的长度是可变的,也就是说,定义一个char[10]和varchar[10],如果存进去的是‘csdn’,那么char所占的长度依然为1 ...

  5. IE浏览器Bug总结

    每每在网上搜索IE浏览器Bug时,总是骂声一片,特别是前端工程师,每天都要面对,IE浏览器特别是IE6,存在很多Bug,对Web标准的支持也拖后腿,但不可否认,IE浏览器是曾经的霸主,它的贡献也是巨大 ...

  6. 一个文档让vim飞起来

    原文地址:http://www.cnblogs.com/songfy/p/5635757.html 引言 今天我们特地来讲讲这个vim的配置. vim这东西, 很多人装逼的时候经常会提到, 不过大部分 ...

  7. Linux 入门记录:十三、Linux 扩展权限

    一.默认权限 每一个终端都有一个 umask 属性,是用来确定新建文件或目录的默认权限的“掩码”(mask 有“掩码”的含义,至于 u,后面说). Linux 中一般有默认的权限掩码,使用命令 uma ...

  8. 经典卷积网络模型 — VGGNet模型笔记

    一.简介 VGGNet是计算机视觉组(Visual Geometry Group)和Google DeepMind公司的研究员一起研究的深度卷积神经网络.VGGNet探索了卷积神经网络深度与性能之间的 ...

  9. shell常见操作整理(更新)

    查看文件第20到30行的内容 法一:[root@oldboy ~]# seq 100 > ett.txt [root@oldboy ~]# head -30 ett.txt | tail -11 ...

  10. echarts3.0版本断点连线的处理

      项目应用到echarts图表组件.官网的demo中出现空数据会断开.经过跟踪调试.修改echarts.js以下代码即可实现断点连线功能(需要将空数据处理成'-'.这样才能均值): for (var ...