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. [bzoj3993][SDOI2015]星际战争-二分+最大流

    Brief Description 3333年,在银河系的某星球上,X军团和Y军团正在激烈地作战.在战斗的某一阶段,Y军团一共派遣了N个巨型机器人进攻X军团的阵地,其中第i个巨型机器人的装甲值为Ai. ...

  2. JAVA list 列表 字典 dict

    import java.util.ArrayList; import java.util.HashMap; import java.util.Map; import java.util.Set; pu ...

  3. bzoj 3197 DP

    这道题我们可以看成给定两个黑白树,可以修改其中一棵树的颜色,问最少修改多少颜色可以使两棵树同构. 首先我们知道在树的同构中树上最长链中点(如果是偶数的话就是中间两个点)是不变的,我们把这个点叫做树的重 ...

  4. git 配置多用户

    .ssh 下的 config.txt 内容 # 配置github.com Host github.com HostName github.com IdentityFile ~/.ssh/id_rsa_ ...

  5. linux 设备树【转】

    转自:http://blog.csdn.net/chenqianleo/article/details/77779439 [-] linux 设备树 为什么要使用设备树Device Tree 设备树的 ...

  6. WAMP Apache 2.5 配置虚拟主机

    1.在 Apache 的安装目录下 conf/httpd.conf 文件中搜索 hosts,去掉 Include 前面的 “#” 号后,即可启用虚拟主机. # Virtual hosts #Inclu ...

  7. bzoj 1015 星球大战starwar

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1015 题解: 如果按照题目的意思,每次删点.删边太困难了……于是采用逆向思维,构造出最后的 ...

  8. jQuery 选中tr下面的第某个td

    1.问题描述 点击 table 中的某行 tr,获取该 tr 下的第一个 td 标签下的< input type="hidden" value="92"/ ...

  9. leetcode 之Remove Duplicates from Sorted List(17)

    很简单的一题,需要注意的是如果某结点重复了记得将其删除. ListNode *deleteDuplicates(ListNode *head) { if (head == nullptr) retur ...

  10. ORM数据库查询操作之基于双下划线的跨表查询

     创建表结构 from django.db import models class Book(models.Model): title=models.CharField(max_length=32) ...