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. Python--day42--mysql创建用户及授权

    1,打开mysql数据库命令:mysql -u root -p 2,创建数据库create database db2; 2,删除数据库drop database db2; 2,展示有多少个文件夹:sh ...

  2. Mac下SVN基本操作和常见错误

    一.基本操作 1  从服务器上下载代码 svn checkout http://xxx.xxx.xxx/xxx 2  获取最新的代码 svn update 3  提交代码 svn commit -m ...

  3. P1072 城市轰炸

    题目描述 一个大小为N*M的城市遭到了X次轰炸,每次都炸了一个每条边都与边界平行的矩形. 在轰炸后,有Y个关键点,指挥官想知道,它们有没有受到过轰炸,如果有,被炸了几次,最后一次是第几轮. 输入格式 ...

  4. P1053 第K小的取法

    题目描述 给定一个含n个数的数组.现在从中取出一些数.并把这些数相加得出一个和,如果有多种取法的和相同,则视为多种取法.求所有取法对应的和中第K小的和. 输入格式 第一行包括两个正整数n(n<= ...

  5. git 常用操作命令(Common operation)

    win10清除已登录账号密码方法 打开控制面板(Control Panel): 选择用户账户(User Accounts): 选择管理你的凭据(Credential Manager): 管理windo ...

  6. 闲来无事写一个jquery计算器,没有进行封装......

    <!doctype html> <html> <head> <meta charset="utf-8" /> <title&g ...

  7. 聊聊多线程哪一些事儿(task)之 二 延续操作

    hello,又见面啦,昨天我们简单的介绍了如何去创建和运行一个task.如何实现task的同步执行.如何阻塞等待task集合的执行完毕等待,昨天讲的是task的最基本的知识点,如果你没有看昨天的博客, ...

  8. select * from user 这条 SQL 语句,背后藏着哪些不可告人的秘密?

    作为一名 Java开发人员,写 SQL 语句是常有的事,但是你知道 SQL 语句背后的处理逻辑吗?比如下面这条 SQL 语句: select * from user where id=1 执行完这条语 ...

  9. node多进程的创建与守护

    node是单线程运行,我们的node项目如何利用多核CPU的资源,同时提高node服务的稳定性呢? 1. node的单线程 进程是一个具有一定独立功能的程序在一个数据集上的一次动态执行的过程,是操作系 ...

  10. 【题解】LOJ2759. 「JOI 2014 Final」飞天鼠(最短路)

    [题解]LOJ2759. 「JOI 2014 Final」飞天鼠(最短路) 考虑最终答案的构成,一定是由很多飞行+一些上升+一些下降构成. 由于在任何一个点上升或者下降代价是一样的,所以: 对于上升操 ...