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程序设计》第一周学习总结

    18个章节的问题 (1)为什么需要JVM让java跨平台? (2)JVM与JDK,与JRE的关系? (3)为什么 -0/3 结果是 0,而 -0.0/3.0 结果是 -0.0?(注意后边的结果0带负号 ...

  2. word-wrap word-break 区别

    word-wrap word-break 区别 word-break * word-break:break-all;//直接把单词截断 * word-break:break-word;//虽然单词截断 ...

  3. 灵活使用ARM汇编的WEAK关键字

    //=====================================================================//TITLE://    灵活使用ARM汇编的WEAK关 ...

  4. devinet_ioctl

    Kernel: 4.12.6 deinet_ioctl:获取或者设置接口的地址,掩码,标记等信息: 注意,使用SIOCSIFFLAGS关闭设备,如果使用了别名,则删除对应ip,如果其为主ip,并且从i ...

  5. selenium===requestium模块介绍

    有时,你可能会在网上实现一些自动化操作.比如抓取网站,进行应用测试,或在网上填表,但又不想使用API,这时自动化就变得很必要.Python提供了非常优秀的Requests库可以辅助进行这些操作.可惜, ...

  6. python基础===Character string

    本文转自:python之Character string 1.python字符串 字符串是 Python 中最常用的数据类型.我们可以使用引号('或")来创建字符串,l Python不支持单 ...

  7. go语言爬虫goquery和grequests的使用

    /*下载工具*/ package main import ( "fmt" //go语言版本的jquery "github.com/PuerkitoBio/goquery& ...

  8. JS中类型检测方式

    在js中的类型检测目前我所知道的是三种方式,分别有它们的应用场景: 1.typeof:主要用于检测基本类型. typeof undefined;//=> undefined typeof 'a' ...

  9. FineReport——登录不到决策系统

    在不断的测试过程中,可能会造成缓存数据的累积,所以在登录过程中可能会出现登录不到决策系统,而是跳转到某一模板页面 解决方法就是清理缓存或者换一个浏览器测试.

  10. python中烦人的锟斤拷(\xef\xbf\xbd)

    首先要知道\xef\xbf\xbd是啥东西 >>> u'\uFFFD'.encode('utf-8') '\xef\xbf\xbd' 由此我们可以知道\xef\xbf\xbd是utf ...