XOR

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1648    Accepted Submission(s): 503

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.

 
Author
elfness
 
维护线性基即可
code:
 #include<cstdio>
#include<iostream>
#include<cmath>
#include<cstring>
#include<algorithm>
#define maxn 10005
using namespace std;
typedef long long int64;
char ch;
int T,q,n;
int64 k,a[maxn];
bool ok;
void read(int &x){
for (ok=,ch=getchar();!isdigit(ch);ch=getchar()) if (ch=='-') ok=;
for (x=;isdigit(ch);x=x*+ch-'',ch=getchar());
if (ok) x=-x;
}
void read(int64 &x){
for (ok=,ch=getchar();!isdigit(ch);ch=getchar()) if (ch=='-') ok=;
for (x=;isdigit(ch);x=x*+ch-'',ch=getchar());
if (ok) x=-x;
}
void gauss(){
int i,j,k;
for (i=,k=;i>=;i--){
for (j=k;j<=n&&!(a[j]&(1LL<<i));j++);
if (j<=n){
swap(a[k],a[j]);
for (j=;j<=n;j++) if (j!=k&&(a[j]&(1LL<<i))) a[j]^=a[k];
k++;
}
}
sort(a+,a+n+),a[]=-;
for (i=,j=;j<=n;j++) if (a[j]!=a[j-]) a[++i]=a[j];
n=i;
}
int64 calc(int64 k){
int64 ans=;
int i=;
if (a[]==) k--,i++;
for (;i<=n&&k;i++,k>>=) if (k&) ans^=a[i];
if (i>n&&k) return -;
return ans;
}
int main(){
read(T);
for (int t=;t<=T;t++){
read(n);
for (int i=;i<=n;i++) read(a[i]);
gauss();
printf("Case #%d:\n",t);
for (read(q);q;q--) read(k),printf("%I64d\n",calc(k));
}
return ;
}

hdu3949的更多相关文章

  1. HDU3949 XOR (线性基)

    HDU3949 XOR Problem Description XOR is a kind of bit operator, we define that as follow: for two bin ...

  2. 【线性基】hdu3949 XOR

    给你n个数,问你将它们取任意多个异或起来以后,所能得到的第K小值? 求出线性基来以后,化成简化线性基,然后把K二进制拆分,第i位是1就取上第i小的简化线性基即可.注意:倘若原本的n个数两两线性无关,也 ...

  3. 【HDU3949】XOR

    [题目大意] 给定一个数组,求这些数组通过异或能得到的数中的第k小是多少. 传送门:http://vjudge.net/problem/HDU-3949 [题解] 首先高斯消元求出线性基,然后将k按照 ...

  4. hdu3949 XOR xor高斯消元

    XOR Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...

  5. hdu3949 异或空间 + 求矩阵的主元

    给定n个整数,将数分解成01序列,由这n个01序列构成矩阵,这n个数构成线性空间,这就是异或空间 将这个矩阵高斯消元,求出t个主元,那么由着t个主元构成的线性空间里总共有2^t个数 设这t个数分别是a ...

  6. HDU3949 XOR

    嘟嘟嘟 集训的时候发现自己不会线性基,就打算学一下. 这东西学了挺长时间,其实不是因为难,而是天天上午考试,下午讲题,结果晚上就开始颓了. 今天总算是有大块的时间好好学了一遍. 这里推荐menci大佬 ...

  7. HDU3949:XOR——题解

    http://acm.hdu.edu.cn/showproblem.php?pid=3949 求n个数的异或和第k小. 参考:https://blog.sengxian.com/algorithms/ ...

  8. [hdu3949]XOR(线性基求xor第k小)

    题目大意:求xor所有值的第k小,线性基模板题. #include<cstdio> #include<cstring> #include<algorithm> #i ...

  9. hdu3949(线性基,求第k小的异或和

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3949 XOR Time Limit: 2000/1000 MS (Java/Others)    Me ...

随机推荐

  1. The game of life(生命游戏)新算法

    我写了一种常见的实现算法,和另一种新算法,即不是每次循环计算每个细胞的周围细胞数来产生下一时刻,而是每次每个产生状态变化的细胞主动通知周围的邻居,因此每个细胞增加一个用来记录邻居数的字段.由邻居数决定 ...

  2. (组合数学3.1.1.2)UVA 10098 Generating Fast(使用字典序思想产生所有序列)

    /* * UVA_10098.cpp * * Created on: 2013年10月8日 * Author: Administrator */ #include <iostream> # ...

  3. Oracle中alter system命令参数之scope

    SCOPE The SCOPE clause lets you specify when the change takes effect. Scope depends on whether you s ...

  4. Java自动装箱和自动拆箱操作

    1.Java数据类型 在介绍Java的自动装箱和拆箱之前,我们先来了解一下Java的基本数据类型. 在Java中,数据类型可以分为两大种,Primitive Type(基本类型)和Reference ...

  5. PHP实现大文件的上传设置

    打开php.ini,首先找到 ;;;;;;;;;;;;;;;; ; File Uploads ; ;;;;;;;;;;;;;;;; 区域,有影响文件上传的以下几个参数: file_uploads = ...

  6. 美丽的表格样式(使用CSS样式表控制表格样式)

    按照WEB2.0风格,设计了几个表格样式,希望大家喜欢. WEB2.0提倡使用div开布局,但不是要全然放弃使用表格,表格在数据展现方面还是不错的选择. 如今使用介绍使用CSS样式表来控制.美化表格的 ...

  7. Java并发学习之二——获取和设置线程信息

    本文是学习网络上的文章时的总结,感谢大家无私的分享. Thread类的对象中保存了一些属性信息可以帮助我们辨别每个线程.知道它的一些信息 ID:每一个线程的独特标示: Name:线程的名称: Prio ...

  8. php 二维数组转换成树状数组(转)

    <?php/** * @param array $list 要转换的结果集 * @param string $pid parent标记字段 * @param string $level leve ...

  9. Android(java)学习笔记214:开源框架的文件上传(只能使用Post)

    1.文件上传给服务器,服务器端必然要写代码进行支持,如下: 我们新建一个FileUpload.jsp的动态网页,同时我们上传文件只能使用post方式(不可能将上传数据拼凑在url路径下),上传数据Ap ...

  10. Linux学习笔记共享

    从学习到现在,已经3个月了,还有不到一个月linux课程就要结束,大概的情况如下: 预科一周,主要是学习了网络,思科的内容 linux基础课程,从无到有 linux shell 脚本 linux项目实 ...