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. H3C IGP与EGP

  2. wpf遮罩~~~(搬运过来的)

    方便自己以后用,原文:https://blog.csdn.net/lwwl12/article/details/78472235 直接上代码 public partial class BaseWind ...

  3. presto,dremio,spark-sql与ranger的整合记录

    dremio,spark-sql,presto和ranger的整合 当前,ranger没有现成的插件来管理dremio,spark-sql,presto. 暂时使用的方法是新建一个用户,如presto ...

  4. neo4j-查询示例

    查找名为"Cloud Atlas"的电影 match(cloudAtlas {title:"Cloud Atlas"}) return cloudAtlas 查 ...

  5. 第二阶段:2.商业需求文档MRD:3.MRD-目标用户分析

    以上是对目标市场的分析! 用户描述是定性.市场统计是定量.用用户分类模型去剖析用户分类. 例子.做百度推广时候的用户分析.不同角色的关注点不同.三个情景:广告售卖,广告投放,分析评估. 用户的使用习惯 ...

  6. DEVOPS技术实践_14:使用docker部署jenkins

    一 基础环境准备 [root@node6 ~]# cat /etc/redhat-release CentOS Linux release (Core) [root@node6 ~]# yum -y ...

  7. 修改kubelet启动参数

    我是用kubeadm安装的k8s,现在通过Aqua扫描出相关配置问题,需要修改kubelet的启动参数: 默认配置文件名为:10-kubeadm.conf #vim /usr/lib/systemd/ ...

  8. final阶段20191121-5 Scrum立会报告+燃尽图 01

    此作业要求参见:http://edu.cnblogs.com/campus/nenu/2019fall/homework/10065 一: 组名:组长组 组长:杨天宇 组员:魏新  罗杨美慧   王歆 ...

  9. Windows Live Writer 语法高亮

    1.WindowsLiveWriter.CNBlogs.CodeHighlighter.rar 这个插件生成的高亮代码与网页上的一模一样,插入后即可立即显示效果,不过貌似它必须联网才能实时显示效果,因 ...

  10. 编辑软件->"Notepad++"

    编辑软件->"Notepad++" Notepad++是什么? Notepad++功能比 Windows 中的Notepad(记事本)强大,除了可以用来制作一般的纯文字说明文 ...