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

  1. 2
  2. 2
  3. 1 2
  4. 4
  5. 1 2 3 4
  6. 3
  7. 1 2 3
  8. 5
  9. 1 2 3 4 5

Sample Output

  1. Case #1:
  2. 1
  3. 2
  4. 3
  5. -1
  6. Case #2:
  7. 0
  8. 1
  9. 2
  10. 3
  11. -1

Hint

  1. If you choose a single number, the result you get is the number you choose.
  2. Using long long instead of int because of the result may exceed 2^31-1.
  3.  
  4. 题意 给你 n 个数 , q 个询问,每次询问第 K 异或小的值
    思路分析 :线性基的板子题,构造出 n 个数的线性基,然后将每一位相互独立出来,然后看有多少个不唯一的,存在一个新的数组里,如果在新的数组中有K个元素,则异或出来的最终答案最多有 2^k ,
       当然这里面是包括 0 的,但是并不是所有的都可以异或出来 0 ,那要怎么确定这个能否异或出来 0 呢?如果构造出来的线性基有 k 位不为 1,那么说明每个数都提供了 1,则说明不会异或出 0 ,否则可以。
    代码示例:
  1. #define ll long long
  2.  
  3. ll n;
  4. ll a[65];
  5.  
  6. void insert(ll x){
  7.  
  8. for(ll i = 60; i >= 0; i--){
  9. if ((1ll<<i)&x){
  10. if (!a[i]) {a[i] = x; return;}
  11. x ^= a[i];
  12. }
  13. }
  14. }
  15. ll cnt = 0;
  16. ll p[65];
  17.  
  18. void rbuild(){
  19. for(ll i = 60; i >= 0; i--){
  20. for(ll j = i-1; j >= 0; j--){
  21. if ((1ll<<j)&a[i]) a[i] ^= a[j];
  22. }
  23. }
  24.  
  25. for(ll i = 0; i <= 60; i++){
  26. if (a[i]) p[cnt++] = a[i];
  27. }
  28. }
  29.  
  30. ll query(ll x){
  31. if (x >= (1ll<<cnt)) return -1;
  32. ll ans = 0;
  33. for(ll j = 60; j >= 0; j--){
  34. if ((1ll<<j)&x){
  35. ans ^= p[j];
  36. }
  37. }
  38. return ans;
  39. }
  40.  
  41. int main() {
  42. //freopen("in.txt", "r", stdin);
  43. //freopen("out.txt", "w", stdout);
  44. ll t, q;
  45. ll x;
  46. ll kase = 1;
  47.  
  48. cin >>t;
  49. while(t--){
  50. memset(a, 0, sizeof(a));
  51. memset(p, 0, sizeof(p));
  52. cnt = 0;
  53. cin >> n;
  54. for(ll i = 1; i <= n; i++){
  55. scanf("%lld", &x);
  56. insert(x);
  57. }
  58. rbuild();
  59. cin >> q;
  60. //printf("cnt = %d\n", cnt);
  61. printf("Case #%d:\n", kase++);
  62. for(ll i = 1; i <= q; i++){
  63. scanf("%lld", &x);
  64. if (cnt != n) x--;
  65. printf("%lld\n", query(x));
  66. }
  67. }
  68. return 0;
  69. }

线性基 - 寻找异或第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. golang http get请求方式

    client := &http.Client{} //生成要访问的url,token是api鉴权,每个api访问方式不同,根据api调用文档拼接URLurl := fmt.Sprintf(&q ...

  2. 使用 node.js三行代码实现手机端访问html页面文件

    首先确保你安装了node (全局安装) npm install -g browser-sync // --files 路径是相对于运行该命令的项目(目录) browser-sync start --s ...

  3. HttpRepl 互操作的 RESTful HTTP 服务调试命令行工具

    今天早上曽根セイラ告诉我一个好用的工具 HttpRepl 这是一个可以在命令行里面对 RESTful 的 HTTP 服务进行路由跳转和访问的命令行工具.可以使用 cd 这个命令和像文件跳转已经跳转到下 ...

  4. Cookie的使用、Cookie详解、HTTP cookies 详解、获取cookie的方法、客户端获取Cookie、深入解析cookie

    Cookie是指某些网站为了辨别用户身份.进行session跟踪而存储在用户本地终端上的数据(通常经过加密),比如说有些网站需要登录才能访问某个页面,在登录之前,你想抓取某个页面内容是不允许的.那么我 ...

  5. ASP.NET WebForm Identity使用

    环境 win10企业版x64+visual studio 2017+.net 4.5 step1 基本使用+邮件确认+密码重置 https://docs.microsoft.com/en-us/asp ...

  6. 006.MFC_对话框_复选框_单选钮

    对话框和控件复选框单选框分组框示例:三原色画图 一.建立名为Demo2的MFC工程,按照下图添加控件 并修改2个Group Box Caption属性分别为颜色.外观 修改3个Check Box Ca ...

  7. elasticsearch基础知识杂记

    日常工作中用到的ES相关基础知识和总结.不足之处请指正,会持续更新. 1.集群的健康状况为 yellow 则表示全部主分片都正常运行(集群可以正常服务所有请求),但是 副本 分片没有全部处在正常状态. ...

  8. 关于python2和python3除法的区别

    在Python2中,除法的取值结果取整数 >>> 9/2 4 而在Python3中,除法/的结果包含小数,如果只想取整数需要使用// >>> 9/2 4.5 > ...

  9. scrf 原理及flask-wtf防护

    了解什么是scrf? SCRF跨站点请求伪造Cross—Site Request Forgery) 指恶意用户通过个人用户的点击,然而盗用用户的账号信息,并发送邮件.虚拟货币的转账,以及一些重要的事务 ...

  10. jsp页面获取当前系统时间

    value="<% out.print(new java.text.SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(n ...