HDU3949 XOR(线性基第k小)
Problem Description
Input
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
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
1 2
4
1 2 3 4
3
1 2 3
5
1 2 3 4 5
Sample Output
1
2
3
-1
Case #2:
0
1
2
3
-1
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.
Source
题目大意:给出$n$个数,问两两异或后第$k$小的数是多少
看了很多篇博客,发现都是在围绕着高斯消元解xor方程组来的。
然后我惊讶的发现,原来高斯消元解xor解方程组其实就是求出线性基然后再消元
通过消元保证线性基内有元素的每一列只有一个$1$
然后把$k$二进制分解,如果第$i$是$1$就异或上第$i$个有解的线性基
同时要特判$0$的情况,若线性基的大小与元素的大小相同则不能异或为$0$(线性无关),否则可以异或为零,这时我们只要求出第$k-1$小就可以了
这里把$k$二进制分解后的$0/1$实际对应了线性基中元素选/不选,可以证明这样一定是对的
#include<cstdio>
#include<cstring>
#include<algorithm>
#define int long long
using namespace std;
const int MAXN = 1e5 + , B = ;
inline int read() {
char c = getchar(); int x = , f = ;
while(c < '' || c > '') {if(c == '-') f = -; c = getchar();}
while(c >= '' && c <= '') x = x * + c - '', c = getchar();
return x * f;
}
int P[MAXN];
void Insert(int x) {
for(int i = B; i >= ; i--) {
if((x >> i) & ) {
if(P[i]) x = x ^ P[i];
else {P[i] = x; return ;}
}
}
}
void Debug(int *a, int N) {
for(int i = ; i <= N; i++) {
for(int j = ; j <= B; j++)
printf("%d ", (P[i] >> j) & );
puts("");
}
puts("********");
}
main() {
int QwQ = read();
for(int test = ; test <= QwQ; test++) {
printf("Case #%I64d:\n", test);
memset(P, , sizeof(P));
int N = read();
for(int i = ; i <= N; i++)
Insert(read());
for(int i = B; i >= ; i--) {
if(P[i]) {
for(int j = i + ; j <= B; j++)
if((P[j] >> i) & ) P[j] ^= P[i];
}
}
int now = ;
for(int i = ; i <= B; i++)
if(P[i])
P[now++] = P[i];
int Q = read();
while(Q--) {
int K = read(), ans = ;
if(now != N) K--;
if(K >= (1ll << now)) {puts("-1"); continue;}
for(int i = ; i <= B; i++)
if((K >> i) & )
ans ^= P[i];
printf("%I64d\n", ans);
}
}
}
HDU3949 XOR(线性基第k小)的更多相关文章
- hdu 3949 XOR 线性基 第k小异或和
题目链接 题意 给定\(n\)个数,对其每一个子集计算异或和,求第\(k\)小的异或和. 思路 先求得线性基. 同上题,转化为求其线性基的子集的第k小异或和. 结论 记\(n\)个数的线性基为向量组\ ...
- HDU3949 XOR (线性基)
HDU3949 XOR Problem Description XOR is a kind of bit operator, we define that as follow: for two bin ...
- [hdu3949]XOR(线性基求xor第k小)
题目大意:求xor所有值的第k小,线性基模板题. #include<cstdio> #include<cstring> #include<algorithm> #i ...
- HDU 3949 XOR (线性基第k小)题解
题意: 给出\(n\)个数,求出子集异或第\(k\)小的值,不存在输出-1. 思路: 先用线性基存所有的子集,然后对线性基每一位进行消元,保证只有\(d[i]\)的\(i\)位存在1,那么这样变成了一 ...
- Xor && 线性基练习
#include <cstdio> #include <cstring> ; ; int cnt,Ans,b,x,n; inline int Max(int x,int y) ...
- HDU 3949 XOR [高斯消元XOR 线性基]
3949冰上走 题意: 给你 N个数,从中取出若干个进行异或运算 , 求最后所有可以得到的异或结果中的第k小值 N个数高斯消元求出线性基后,设秩为$r$,那么总共可以组成$2^r$中数字(本题不能不选 ...
- hdu 3949 XOR (线性基)
链接: http://acm.hdu.edu.cn/showproblem.php?pid=3949 题意: 给出n个数,从中任意取几个数字异或,求第k小的异或和 思路: 线性基求第k小异或和,因为题 ...
- BZOJ4269:再见Xor(线性基)
Description 给定N个数,你可以在这些数中任意选一些数出来,每个数可以选任意多次,试求出你能选出的数的异或和的最大值和严格次大值. Input 第一行一个正整数N. 接下来一行N个非负整数. ...
- HDU 3949 XOR 线性基
http://acm.hdu.edu.cn/showproblem.php?pid=3949 求异或第k小,结论是第k小就是 k二进制的第i位为1就把i位的线性基异或上去. 但是这道题和上一道线性基不 ...
随机推荐
- MVC系统学习2—MVC路由
在MVC下不是通过对物理文件的映射来实行访问的,而是通过定义后的路由Url来实现访问的.在前一篇讲到我们是在全局文件下进行路由配置. routes.MapRoute( & ...
- better-scroll & scroll
scroll better-scroll https://github.com/ustbhuangyi/better-scroll/blob/master/README.md#getting-star ...
- HDU 1325 拓扑排序
根据题目所给的3个不符合情况的条件,一个个判断图是否符合这3个条件即可 1.不能出现内部环,拓扑排序判断 2.不能有超过1个点的入度为0,因为只有一个树根 3.每个点最多一个入度 这里要注意的一点是这 ...
- Uva1103 Ancient Messages
题意:识别图中的象形文字.但是,图形可以任意的拉伸,但不能拉断. 分析:这种题如果图形没有特征是不可做类型的题,不过观察图形可以发现每个图形中的洞的数量是一定的,我们只需要数出每一个封闭图形的洞数就能 ...
- A^B Mod C
基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题 给出3个正整数A B C,求A^B Mod C. 例如,3 5 8,3^5 Mod 8 = 3. Input 3个正整 ...
- xcode5下取消ARC
打开你的工程,点击目录的工程文件,最顶端蓝色的,然后选择project下你的工程,还是蓝色那项,然后build Settings,然后往下拉,在Apple LLVM 5.0 - Language - ...
- vue2源码浏览分析02
1.组件初始化方法 init Vue.prototype._init = function (options) { /* istanbul ignore if */ if ("develop ...
- 错误代码: 1045 Access denied for user 'skyusers'@'%' (using password: YES)
1. 错误描写叙述 GRANT ALL PRIVILEGES ON *.* TO root@"%" IDENTIFIED BY "."; 1 queries e ...
- Android-spinner
Android-spinner 一 Adapter概念 spinner:下拉栏里面的TextView 指定系统spinner:在xml文件中面的spinner中的属性android:entries=& ...
- scikit-learn: isotonic regression(保序回归,非常有意思,仅做知识点了解,但差点儿没用到过)
http://scikit-learn.org/stable/auto_examples/plot_isotonic_regression.html#example-plot-isotonic-reg ...