题目链接: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. Python之常用模块(二)

    shelve xml处理 configparser hashlib logging   shelve模块 shelve是一个简单的k,v将内存数据通过文件持久化的模块,可以持久化任何pickle可支持 ...

  2. 人物-IT-张志东:张志东

    ylbtech-人物-IT-张志东:张志东 张志东,广东东莞人,腾讯创办人之一,腾讯高级副总裁兼科技总裁,于1993年取得深圳大学理学学士学位,并于1996年取得华南理工大学计算机应用及系统架构硕士学 ...

  3. C#事件触发机制

    C#的事件触发机制,类似于c++的回调函数机制 我先简单说一下,委托和事件的实质,后期再重开一篇博文来详细说 委托:指向方法的指针,类似于C的函数指针 事件:是一个可以存放0个或多个方法指针的数据结构 ...

  4. [cf839d]Winter is here容斥原理

    题意:给定一个数列${a_i}$,若子序列长度为$k$,最大公约数为$gcd$,定义子序列的权值为$k*\gcd (\gcd  > 1)$.求所有子序列的权值和. 答案对10^9+7取模. 解题 ...

  5. JavaScript的内部对象

    JavaScript的内部对象 按创建方式不同分为:使用变量声明的隐性对象,使用new创建的显性对象 隐性对象 在赋值和声明后就是一个隐性对象,隐性对象不支持prototype属性,也无法随意扩展对象 ...

  6. 向PCD文件写入点云数据

    博客转载自:http://www.pclcn.org/study/shownews.php?lang=cn&id=83 本小节我们学习如何向PCD文件写入点云数据. 代码 章例2文件夹中,打开 ...

  7. LightOJ 1311 Unlucky Bird (物理题)

    题意:有点长,意思是说有一个鸟,在两列火车之间不停的来回飞,两列相距为d时,都开始减速,直到最后停止下来,正好是相距0米, 现在给定两列车的速度和减速时的加速度,和鸟的速度求 d 和鸟飞过的路程. 析 ...

  8. htmlparser API

    htmlparser所有的filter htmlparser所有的Tags htmlparser API: http://htmlparser.sourceforge.net/javadoc/inde ...

  9. Spark 各个组件关系

    Term Meaning Application User program built on Spark. Consists of a driver program and executors on ...

  10. 序列化 (C#)

    序列化是指将对象转换成字节流,从而存储对象或将对象传输到内存.数据库或文件的过程. 它的主要用途是保存对象的状态,以便能够在需要时重新创建对象. 反向过程称为"反序列化". 序列化 ...