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

Source

题目大意:给出$n$个数,问两两异或后第$k$小的数是多少

看了很多篇博客,发现都是在围绕着高斯消元解xor方程组来的。

然后我惊讶的发现,原来高斯消元解xor解方程组其实就是求出线性基然后再消元

通过消元保证线性基内有元素的每一列只有一个$1$

然后把$k$二进制分解,如果第$i$是$1$就异或上第$i$个有解的线性基

同时要特判$0$的情况,若线性基的大小与元素的大小相同则不能异或为$0$(线性无关),否则可以异或为零,这时我们只要求出第$k-1$小就可以了

这里把$k$二进制分解后的$0/1$实际对应了线性基中元素选/不选,可以证明这样一定是对的

  1. #include<cstdio>
  2. #include<cstring>
  3. #include<algorithm>
  4. #define int long long
  5. using namespace std;
  6. const int MAXN = 1e5 + , B = ;
  7. inline int read() {
  8. char c = getchar(); int x = , f = ;
  9. while(c < '' || c > '') {if(c == '-') f = -; c = getchar();}
  10. while(c >= '' && c <= '') x = x * + c - '', c = getchar();
  11. return x * f;
  12. }
  13. int P[MAXN];
  14. void Insert(int x) {
  15. for(int i = B; i >= ; i--) {
  16. if((x >> i) & ) {
  17. if(P[i]) x = x ^ P[i];
  18. else {P[i] = x; return ;}
  19. }
  20. }
  21. }
  22. void Debug(int *a, int N) {
  23. for(int i = ; i <= N; i++) {
  24. for(int j = ; j <= B; j++)
  25. printf("%d ", (P[i] >> j) & );
  26. puts("");
  27. }
  28. puts("********");
  29. }
  30. main() {
  31. int QwQ = read();
  32. for(int test = ; test <= QwQ; test++) {
  33. printf("Case #%I64d:\n", test);
  34. memset(P, , sizeof(P));
  35. int N = read();
  36. for(int i = ; i <= N; i++)
  37. Insert(read());
  38. for(int i = B; i >= ; i--) {
  39. if(P[i]) {
  40. for(int j = i + ; j <= B; j++)
  41. if((P[j] >> i) & ) P[j] ^= P[i];
  42. }
  43. }
  44. int now = ;
  45. for(int i = ; i <= B; i++)
  46. if(P[i])
  47. P[now++] = P[i];
  48. int Q = read();
  49. while(Q--) {
  50. int K = read(), ans = ;
  51. if(now != N) K--;
  52. if(K >= (1ll << now)) {puts("-1"); continue;}
  53. for(int i = ; i <= B; i++)
  54. if((K >> i) & )
  55. ans ^= P[i];
  56. printf("%I64d\n", ans);
  57. }
  58. }
  59. }

HDU3949 XOR(线性基第k小)的更多相关文章

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

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

  2. HDU3949 XOR (线性基)

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

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

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

  4. HDU 3949 XOR (线性基第k小)题解

    题意: 给出\(n\)个数,求出子集异或第\(k\)小的值,不存在输出-1. 思路: 先用线性基存所有的子集,然后对线性基每一位进行消元,保证只有\(d[i]\)的\(i\)位存在1,那么这样变成了一 ...

  5. Xor && 线性基练习

    #include <cstdio> #include <cstring> ; ; int cnt,Ans,b,x,n; inline int Max(int x,int y) ...

  6. HDU 3949 XOR [高斯消元XOR 线性基]

    3949冰上走 题意: 给你 N个数,从中取出若干个进行异或运算 , 求最后所有可以得到的异或结果中的第k小值 N个数高斯消元求出线性基后,设秩为$r$,那么总共可以组成$2^r$中数字(本题不能不选 ...

  7. hdu 3949 XOR (线性基)

    链接: http://acm.hdu.edu.cn/showproblem.php?pid=3949 题意: 给出n个数,从中任意取几个数字异或,求第k小的异或和 思路: 线性基求第k小异或和,因为题 ...

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

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

  9. HDU 3949 XOR 线性基

    http://acm.hdu.edu.cn/showproblem.php?pid=3949 求异或第k小,结论是第k小就是 k二进制的第i位为1就把i位的线性基异或上去. 但是这道题和上一道线性基不 ...

随机推荐

  1. Android layer-list(1)

     Android layer-list(1) Android layer-list,顾名思义,实现列表组合后形成的图层,写一个例子. activity_main.xml文件: <?xml v ...

  2. POJ 1019 数学题

    #include <cstdio> #include <cstring> using namespace std; ]; //sum[i]表示尾数为i的组最大可达到的数字个数 ...

  3. 稍微成型点的用WEBSOCKET实现的实时日志LOG输出

    难的是还是就地用JS显示出来相关的发布进度. 还好,花了一下午实现了. 可以移植到项目中去罗... websocket.py: import tornado.ioloop import tornado ...

  4. POJ 1523 SPF 割点 Tarjan

    SPF Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 9317   Accepted: 4218 Description C ...

  5. S - Arc of Dream 矩阵快速幂

    An Arc of Dream is a curve defined by following function: where a 0 = A0 a i = a i-1*AX+AY b 0 = B0  ...

  6. T5090 众数 codevs

    http://codevs.cn/problem/5090/ 时间限制: 1 s  空间限制: 1000 KB  题目等级 : 青铜 Bronze 题目描述 Description 由文件给出N个1到 ...

  7. Codeforces 314B(倍增)

    题意:[a,b]表示将字符串a循环写b遍,[c,d]表示把字符串c循环写d遍,给定a,b,c,d,求一个最大的p,使得[[c,d],p]是[a,b]的子序列(注意不是子串,也就是不要求连续).(b,d ...

  8. CString、char*与string的区别

    三者的区别 CString 是MFC或者ATL中的实现: string 是C++标准库中的实现: char* 为C编程中最常用的字符串指针,一般以’\0’为结束标志. string和CString均是 ...

  9. 【python】蛋疼的中文乱码解决方案

    转自: http://yooooo.us/2013/python-encoding-decoding?variant=zh-cn

  10. MVC中动作方法三个特性以及解决同名方法冲突

    一.Http请求谓词特性(解决方法同名冲突问题的一个方案) 关于Http谓词特点:经常使用,如果不加上该特性,默认动作方法接收所有谓词的请求一般开发中都会加上谓词,限定请求谓词类型 二.NonActi ...