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. UIPageControl---iOS-Apple苹果官方文档翻译

    本系列所有开发文档翻译链接地址:iOS7开发-Apple苹果iPhone开发Xcode官方文档翻译PDF下载地址 //转载请注明出处--本文永久链接:http://www.cnblogs.com/Ch ...

  2. css3_box-shadow使用记录

    1.box-shadow这个属性有6个参数可设置,使用的时候比较少用,每次使用的时候都会忘记,故写此文作记录. 样式: /*1.添加此属性添加阴影*/ box-shadow: 0 0 10px 10p ...

  3. Kill windows和linux 进程

    Windows

  4. mysql增删

    create table msg (id int, name varchar(10)); 插入语句 insert into msg values(1,'root'); insert into msg( ...

  5. mysql 之修改初始密码

    转载自:https://www.cnblogs.com/ivictor/p/5142809.html 为了加强安全性,MySQL5.7为root用户随机生成了一个密码,在error log中,关于er ...

  6. Linux汇编教程02:编写第一个汇编程序

    学习一门语言,最好的方式就是在运用中学习,那么在这一章节中,我们开始编写我们的第一个汇编程序.当然作为第一个程序,其实十分的简单,但可以给大家一个基本的轮廓,了解汇编大概是这样的. 我们这个程序实际上 ...

  7. SD卡 模拟SPI总线控制流程

    SD卡为移动设备提供了安全的,大容量存储解决方法.它本身可以通过两种总线模式和MCU进行数据传输,一种是称为SD BUS的4位串行数据模式,另一种就是大家熟知的4线SPI Bus模式.一些廉价,低端的 ...

  8. python基础===python基础知识问答(转)

    1.到底什么是Python?你可以在回答中与其他技术进行对比 Python是一种解释型语言.与C语言和C的衍生语言不同,Python代码在运行之前不需要编译.其他解释型语言还包括PHP和Ruby. P ...

  9. (二十一)Makefile例子

    ROOT_PROJECT = .DIR_INC = -I$(ROOT_PROJECT)/include -I$(ROOT_PROJECT)/include/NE10 DIR_BIN = $(ROOT_ ...

  10. 设计模式之笔记--抽象工厂模式(Abstract Factory)

    抽象工厂模式(Abstract Factory) 定义 抽象工厂模式(Abstract Factory),提供一个创建一系列相关或相互依赖对象的接口,而无需指定它们具体的类. 类图 描述 多个抽象产品 ...