线性基 - 寻找异或第K大
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大的更多相关文章
- [经典算法题]寻找数组中第K大的数的方法总结
[经典算法题]寻找数组中第K大的数的方法总结 责任编辑:admin 日期:2012-11-26 字体:[大 中 小] 打印复制链接我要评论 今天看算法分析是,看到一个这样的问题,就是在一堆数据 ...
- 寻找数组中第K大的数
给定一个数组A,要求找到数组A中第K大的数字.对于这个问题,解决方案有不少,此处我只给出三种: 方法1: 对数组A进行排序,然后遍历一遍就可以找到第K大的数字.该方法的时间复杂度为O(N*logN) ...
- sgu 275 To xor or not to xor 线性基 最大异或和
题目链接 题意 给定\(n\)个数,取其中的一个子集,使得异或和最大,求该最大的异或和. 思路 先求得线性基. 则求原\(n\)个数的所有子集的最大异或和便可转化成求其线性基的子集的最大异或和. 因为 ...
- 寻找数列中第k大的数算法分析
问题描述:给定一系列数{a1,a2,...,an},这些数无序的,现在求第k大的数. 看到这个问题,首先想到的是先排序,然后直接输出第k大的数,于是得到啦基于排序的算法 算法一: #include&l ...
- hdu 3949 XOR 线性基 第k小异或和
题目链接 题意 给定\(n\)个数,对其每一个子集计算异或和,求第\(k\)小的异或和. 思路 先求得线性基. 同上题,转化为求其线性基的子集的第k小异或和. 结论 记\(n\)个数的线性基为向量组\ ...
- Loj 114 k大异或和
Loj 114 k大异或和 构造线性基时有所变化.试图构造一个线性基,使得从高到低位走,异或上一个非 \(0\) 的数,总能变大. 构造时让任意两个 \(bas\) 上有值的 \(i,j\) ,满足 ...
- BZOJ4671 异或图(容斥+线性基)
题意 定义两个结点数相同的图 \(G_1\) 与图 \(G_2\) 的异或为一个新的图 \(G\) ,其中如果 \((u, v)\) 在 \(G_1\) 与 \(G_2\) 中的出现次数之和为 \(1 ...
- BZOJ4269:再见Xor(线性基)
Description 给定N个数,你可以在这些数中任意选一些数出来,每个数可以选任意多次,试求出你能选出的数的异或和的最大值和严格次大值. Input 第一行一个正整数N. 接下来一行N个非负整数. ...
- F. Ivan and Burgers(线性基,离线)
题目链接:http://codeforces.com/contest/1100/problem/F 题目大意:首先输入n,代表当前有n个数,然后再输入m,代表m次询问,每一次询问是询问区间[l,r], ...
随机推荐
- scrapdy部署爬虫项目
原文:https://blog.csdn.net/JLaiRen/article/details/82902321 scrapyd安装 打开命令行工具输入命令:pip install scrapyd ...
- linux alloc_pages 接口
为完整起见, 我们介绍另一个内存分配的接口, 尽管我们不会准备使用它直到 15 章. 现 在, 能够说 struct page 是一个描述一个内存页的内部内核结构. 如同我们将见到的, 在内核中有许多 ...
- Github上的英文解释
1.AFAIK: As far as I know. 据我所知 2.SPOF: Single point of failure. 单节点崩溃 3.ASAP: As soon as possible. ...
- Linux 内核
在接口总线领域的最新的一项是外部总线的整个类. 这包括 USB, 固件, 和 IEEE1284(基 于并口的外部总线). 这些接口有些类似于老的非外部的技术, 例如 PCMCIA/CardBus 和 ...
- 2018-2-13-git-合并两个仓库
title author date CreateTime categories git 合并两个仓库 lindexi 2018-2-13 17:23:3 +0800 2018-2-13 17:23:3 ...
- Kakuro Extension HDU - 3338 (Dinic)
Kakuro puzzle is played on a grid of "black" and "white" cells. Apart from the t ...
- 网易Java高级开发课程随笔
java学习也有6个月之久,记录下课程相关知识点,目前我还没有掌握,so仅作技术点记录 鉴于在.NET上我封装了一套开发框架,虽去年按.NET封装的思路自己也弄了个java开发框架,还是感觉对java ...
- HFile v2 v3文件结构
http://blog.csdn.net/map_lixiupeng/article/details/40861791 http://blog.csdn.net/map_lixiupeng/artic ...
- 第二阶段:4.商业需求文档MRD:4.PRD-用例和规则
类似之前的泳道图 可以在下面添加一些描述 有时候用图还是会有一些限制 不能够很好的表达
- Z字形变换 leetcode 6
一.按行存储 1.解题思路 1.通过当前行的不断上下循环移动 2.将字符按序存放入vector中 3.最后再按行取出 2.代码及注释 class Solution { public: string c ...