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.
InputFirst 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.OutputFor 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. 题意 : 给你 n 个数 , q 个询问,每次询问第 K 异或小的值
思路分析 :线性基的板子题,构造出 n 个数的线性基,然后将每一位相互独立出来,然后看有多少个不唯一的,存在一个新的数组里,如果在新的数组中有K个元素,则异或出来的最终答案最多有 2^k ,
   当然这里面是包括 0 的,但是并不是所有的都可以异或出来 0 ,那要怎么确定这个能否异或出来 0 呢?如果构造出来的线性基有 k 位不为 1,那么说明每个数都提供了 1,则说明不会异或出 0 ,否则可以。
代码示例:
#define ll long long

ll n;
ll a[65]; void insert(ll x){ for(ll i = 60; i >= 0; i--){
if ((1ll<<i)&x){
if (!a[i]) {a[i] = x; return;}
x ^= a[i];
}
}
}
ll cnt = 0;
ll p[65]; void rbuild(){
for(ll i = 60; i >= 0; i--){
for(ll j = i-1; j >= 0; j--){
if ((1ll<<j)&a[i]) a[i] ^= a[j];
}
} for(ll i = 0; i <= 60; i++){
if (a[i]) p[cnt++] = a[i];
}
} ll query(ll x){
if (x >= (1ll<<cnt)) return -1;
ll ans = 0;
for(ll j = 60; j >= 0; j--){
if ((1ll<<j)&x){
ans ^= p[j];
}
}
return ans;
} int main() {
//freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
ll t, q;
ll x;
ll kase = 1; cin >>t;
while(t--){
memset(a, 0, sizeof(a));
memset(p, 0, sizeof(p));
cnt = 0;
cin >> n;
for(ll i = 1; i <= n; i++){
scanf("%lld", &x);
insert(x);
}
rbuild();
cin >> q;
//printf("cnt = %d\n", cnt);
printf("Case #%d:\n", kase++);
for(ll i = 1; i <= q; i++){
scanf("%lld", &x);
if (cnt != n) x--;
printf("%lld\n", query(x));
}
}
return 0;
}

线性基 - 寻找异或第K大的更多相关文章

  1. [经典算法题]寻找数组中第K大的数的方法总结

    [经典算法题]寻找数组中第K大的数的方法总结 责任编辑:admin 日期:2012-11-26   字体:[大 中 小] 打印复制链接我要评论   今天看算法分析是,看到一个这样的问题,就是在一堆数据 ...

  2. 寻找数组中第K大的数

    给定一个数组A,要求找到数组A中第K大的数字.对于这个问题,解决方案有不少,此处我只给出三种: 方法1: 对数组A进行排序,然后遍历一遍就可以找到第K大的数字.该方法的时间复杂度为O(N*logN) ...

  3. sgu 275 To xor or not to xor 线性基 最大异或和

    题目链接 题意 给定\(n\)个数,取其中的一个子集,使得异或和最大,求该最大的异或和. 思路 先求得线性基. 则求原\(n\)个数的所有子集的最大异或和便可转化成求其线性基的子集的最大异或和. 因为 ...

  4. 寻找数列中第k大的数算法分析

    问题描述:给定一系列数{a1,a2,...,an},这些数无序的,现在求第k大的数. 看到这个问题,首先想到的是先排序,然后直接输出第k大的数,于是得到啦基于排序的算法 算法一: #include&l ...

  5. hdu 3949 XOR 线性基 第k小异或和

    题目链接 题意 给定\(n\)个数,对其每一个子集计算异或和,求第\(k\)小的异或和. 思路 先求得线性基. 同上题,转化为求其线性基的子集的第k小异或和. 结论 记\(n\)个数的线性基为向量组\ ...

  6. Loj 114 k大异或和

    Loj 114 k大异或和 构造线性基时有所变化.试图构造一个线性基,使得从高到低位走,异或上一个非 \(0\) 的数,总能变大. 构造时让任意两个 \(bas\) 上有值的 \(i,j\) ,满足 ...

  7. BZOJ4671 异或图(容斥+线性基)

    题意 定义两个结点数相同的图 \(G_1\) 与图 \(G_2\) 的异或为一个新的图 \(G\) ,其中如果 \((u, v)\) 在 \(G_1\) 与 \(G_2\) 中的出现次数之和为 \(1 ...

  8. BZOJ4269:再见Xor(线性基)

    Description 给定N个数,你可以在这些数中任意选一些数出来,每个数可以选任意多次,试求出你能选出的数的异或和的最大值和严格次大值. Input 第一行一个正整数N. 接下来一行N个非负整数. ...

  9. F. Ivan and Burgers(线性基,离线)

    题目链接:http://codeforces.com/contest/1100/problem/F 题目大意:首先输入n,代表当前有n个数,然后再输入m,代表m次询问,每一次询问是询问区间[l,r], ...

随机推荐

  1. 浅谈集合框架四——集合扩展:集合循环输出方式及list输出方式的效率对比

    最近刚学完集合框架,想把自己的一些学习笔记与想法整理一下,所以本篇博客或许会有一些内容写的不严谨或者不正确,还请大神指出.初学者对于本篇博客只建议作为参考,欢迎留言共同学习. 之前有介绍集合框架的体系 ...

  2. (超级详细版)利用ThinkPHP3.2.3+PHPExcel实现将表格数据导入到数据库

    请先阅读以下步骤再到结尾下载源码 第一步:下载 thinkphp_3.2.3 和 PHPExcel_1.8.0 并解压 对应的网站分别为: http://www.thinkphp.cn/down.ht ...

  3. SpringSide 3 中的安全框架

    在SpringSide 3的官方文档中,说安全框架使用的是Spring Security 2.0.乍一看,吓了我一跳,以为Acegi这么快就被淘汰了呢.上搜索引擎一搜,发现原来Spring Secur ...

  4. vue-lazyload: 想弃坑,但没有找到合适的替代品

    vue-lazyload,相信在vue项目中大家都有用到过它,同时也遇到过大大小小的坑.笔者也遇到过这样一个bug,在一个图片列表页面中,总有一定的概率图片的状态为load,导致图片一直加载中...这 ...

  5. java 反射实现框架功能

    框架与框架要解决的核心问题 我做房子卖给用户住,由用户自己安装门窗和空调,我做的房子就是框架,用户需要使用我的框架,把门窗插入进我提供的框架中.框架与工具类有区别,工具类被用户的类调用,而框架则是调用 ...

  6. 开源项目使用 appveyor 自动构建

    我写了几个开源项目,我想要有小伙伴提交的时候自动运行单元测试,自动运行编译,这样可以保证小伙伴提交清真的代码 本文将会告诉大家如何接入 appveyor 自动构建方案,在 Github 上给自己的开源 ...

  7. js的cookie操作及知识点详解

    <html> <head> <script type="text/javascript"> function getCookie(c_name) ...

  8. css隐藏滚动条、移动端滚动卡顿的解决

    1.如果想保持容器能够滚动,同时不想看到丑陋的滚动条,chrome.firefox和移动端上不考虑兼容性直接 element::-webkit-scrollbar{ display:none } 2. ...

  9. 012.MFC_ListControl

    列表控件CListCtrl 四种视图:大图标 .小图标.列表.详细信息CImageList

  10. 使用 Visual Studio Code 进行远程开发

    使用 Visual Studio Code 进行远程开发 在完成了 AT 指令入门的学习之后,接下来就要使用 AT 指令进行 Socket 通信了.问题在于,之前 .NET 的 Socket 编程只需 ...