题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3949

XOR

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4731    Accepted Submission(s): 1658

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
 
Recommend
xubiao   |   We have carefully selected several similar problems for you:  3946 3948 3947 3945 3944 
 
题目大意:输入t,t组样例,输入n,有n个数,接下来为n个数的值,输入q,代表q次询问,接下来q个数,要你求出第k小的数
学习:https://www.cnblogs.com/vb4896/p/6149022.html
思路:这一题显然是线性基来做,首先先把最大线性无关组,也就是线性基求出来,然后题目要求是要你求出第k小的数,在线性基的基础上,我们可以稍微改造一下,就是保证只有b[i]的第i位是1,其它的第i
位都为0,有了这个性质,我们要求第k小的数,就是把k用二进制表示,如果第j为是1的话,就异或第j个线性基的向量,这里可能需要的读者多想一下,(因为都是二进制表示,具体的我也描述不出来)
看代码:

#include<iostream>
#include<string.h>
#include<map>
#include<cstdio>
#include<cstring>
#include<stdio.h>
#include<cmath>
#include<ctype.h>
#include<math.h>
#include<algorithm>
#include<set>
#include<queue>
typedef long long ll;
using namespace std;
const ll mod=1e9+;
const int maxn=1e4+;
const int maxk=5e3+;
const int maxx=1e4+;
const ll maxe=+;
#define INF 0x3f3f3f3f3f3f
#define Lson l,mid,rt<<1
#define Rson mid+1,r,rt<<1|1
ll a[maxn],b[];
ll m,k;
void guass(int n)
{
memset(b,,sizeof(b));
for(int i=;i<n;i++)
{
for(int j=;j>=;j--)
{
if((a[i]>>j)&)
{
if(!b[j])
{
b[j]=a[i];
break;
}
else
{
a[i]^=b[j];
}
}
}
}
for(int i=;i>=;i--)
{
if(!b[i]) continue;
for(int j=i+;j<=;j++)
{
if((b[j]>>i)&) b[j]^=b[i];//为了使得只有b[i]的第i为1,其它的都不为1
}
}
m=;
for(int i=;i<=;i++) if(b[i]) b[m++]=b[i];
}
int main()
{
int t,ca=,n,q;
cin>>t;
while(t--)
{
cin>>n;
printf("Case #%d:\n",ca++);
for(int i=;i<n;i++)
{
cin>>a[i];
}
guass(n);
cin>>q;
while(q--)
{
ll ans=;
cin>>k;
if(n!=m) k--;//代表可以是0
if(k>=(1ll<<m)) cout<<"-1"<<endl;
else
{
for(int i=;i<=;i++) if((k>>i)&) ans^=b[i];
cout<<ans<<endl;
}
}
}
return ;
}

hdu3949(线性基,求第k小的异或和的更多相关文章

  1. 线性基求第k小异或值

    题目链接 题意:给由 n 个数组成的一个可重集 S,每次给定一个数 k,求一个集合 \(T \subseteq S\), 使得集合 T 在 S 的所有非空子集的不同的异或和中, 其异或和 \(T_1 ...

  2. 算法导论学习之线性时间求第k小元素+堆思想求前k大元素

    对于曾经,假设要我求第k小元素.或者是求前k大元素,我可能会将元素先排序,然后就直接求出来了,可是如今有了更好的思路. 一.线性时间内求第k小元素 这个算法又是一个基于分治思想的算法. 其详细的分治思 ...

  3. 树状数组求第k小的元素

    int find_kth(int k) { int ans = 0,cnt = 0; for (int i = 20;i >= 0;i--) //这里的20适当的取值,与MAX_VAL有关,一般 ...

  4. hdu 4217 Data Structure? 树状数组求第K小

    Data Structure? Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) ...

  5. 线性基求交(2019牛客国庆集训派对day4)

    题意:https://ac.nowcoder.com/acm/contest/1109/C 问你有几个x满足A,B集合都能XOR出x. 思路: 就是线性基求交后,有几个基就是2^几次方. #defin ...

  6. 求第k小的数

    题目链接:第k个数 题意:求n个数中第k小的数 题解: //由快速排序算法演变而来的快速选择算法 #include<iostream> using namespace std; const ...

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

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

  8. UVA11525 Permutation[康托展开 树状数组求第k小值]

    UVA - 11525 Permutation 题意:输出1~n的所有排列,字典序大小第∑k1Si∗(K−i)!个 学了好多知识 1.康托展开 X=a[n]*(n-1)!+a[n-1]*(n-2)!+ ...

  9. *HDU2852 树状数组(求第K小的数)

    KiKi's K-Number Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)T ...

随机推荐

  1. 桥接以及Mercury MW54R中继

    家里连个路由器,一个是比较先进的TP-Link的TL-WR842N(100M),另外一个是比较古老的水星(Mercury) MW54R(54M),我们知道新的路由器都有WDS功能,方便作为副路由器(中 ...

  2. BZOJ1972:[SDOI2010]猪国杀

    我对模拟的理解:https://www.cnblogs.com/AKMer/p/9064018.html 题目传送门:https://www.lydsy.com/JudgeOnline/problem ...

  3. [转]Unity3D学习笔记(四)天空、光晕和迷雾

    原文地址:http://bbs.9ria.com/thread-186942-1-1.html 作者:江湖风云 六年前第一次接触<魔兽世界>的时候,被其绚丽的画面所折服,一个叫做贫瘠之地的 ...

  4. C++STL库中map容器常用应用

    #include<iostream> #include<cstdio> #include<map> //按键值大小构成二叉搜索树 using namespace s ...

  5. shell解决DOS攻击生产案例

    解决DOS攻击生产案例企业实战题5:请用至少两种方法实现!写一个脚本解决DOS攻击生产案例.提示:根据web日志或者或者网络连接数,监控当某个IP并发连接数或者短时内PV达到100,即调用防火墙命令封 ...

  6. [bzoj1568]李超线段树模板题(标志永久化)

    题意:要求在平面直角坐标系下维护两个操作: 1.在平面上加入一条线段.记第i条被插入的线段的标号为i. 2.给定一个数k,询问与直线 x = k相交的线段中,交点最靠上的线段的编号. 解题关键:注意标 ...

  7. UGUI解决嵌套使用多个ScrollRect时的Drag拖动冲突问题

    很简单,直接看代码: using UnityEngine.UI; using UnityEngine.EventSystems; using UnityEngine; /// <summary& ...

  8. python的pip 安装

    python的pip 安装 python有很多好用的包,但是需要的时候一一安装实在是麻烦,还好有pip这么好用的安装工具.所以第一步是安装pip,然后其它软件都so easy! 文章来源:https: ...

  9. JavaScript学习系列8 - JavaScript中的关系运算符

    JavaScript中有8个关系运算符,分别是 ===, !===, ==, !=, <, <=, >, >= 1. 恒等运算符 (===) ===也叫做 严格相等运算符,它要 ...

  10. JavaScript中创建对象的三种模式

    JS中,便于批量创建对象的三种模式: 1.工厂模式:用一个函数封装创建对象的细节,传入必要的参数,在函数内部new一个对象并返回. 缺点:创建的对象无法识别类型(全是Object) 2.构造函数模式: ...