题目链接: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. 代码实现跟控制器跳转到storyBoard

  2. python文件操作 seek(),tell()

    seek():移动文件读取指针到指定位置 tell():返回文件读取指针的位置 seek()的三种模式: (1)f.seek(p,0)  移动当文件第p个字节处,绝对位置 (2)f.seek(p,1) ...

  3. 【转】Pro Android学习笔记(三六):Fragment(1):基本概念

    目录(?)[-] 为何引入Fragment 大小屏幕的适配 横屏竖屏切换 返回键 什么是Fragment 为何引入Fragment 我们之前的Activity都是都是全屏处理较为简单的单一事务功能,适 ...

  4. window下rails4.1 发生TZInfo::DataSourceNotFound 错误 - smallbottle

    在官网上学习rails 4.1 ,启动rails server之后发生了如下错误 $ rails server Booting WEBrick Rails 4.1.0 application star ...

  5. 11_adb指令练习

    通过adb指令咱们装相关的项目.把项目推到设备上.也可以进行文件相关的操作.adb的一些相关的指令. 开启连接IDE和设备的服务. adb可以安装应用也可以卸载应用.项目怎么去区分?一个包名一个是签名 ...

  6. Servlet编程实例 续3

    ----------------siwuxie095 Servlet 跳转之请求的转发 修改 LoginServlet.java: package com.siwuxie095.servlet; im ...

  7. 树莓派 Learning 003 --- GPIO 001 --- 点亮LED

    树莓派 Learning 003 - GPIO 001 - 点亮LED 我的树莓派型号:Raspberry Pi 2 Model B V1.1 装机系统:NOOBS v1.9.2 树莓派 Learni ...

  8. unreal3控制台窗口属性调整

    在windows平台上,unreal3的console窗口类是FOutputDeviceConsoleWindows 启动时,它可以从XXXGame.ini中读取诸如窗口大小之类的属性,具体的代码在 ...

  9. 8.Struts2-057漏洞复现

    漏洞信息: 定义XML配置时如果namespace值未设置且上层动作配置(Action Configuration)中未设置或用通配符namespace时可能会导致远程代码执行. url标签未设置va ...

  10. MapperScan的工作,Spring-Mybatis怎么自动getMapper

    @MapperScan @Import(MapperScannerRegistrar.class) @Repeatable(MapperScans.class) public @interface M ...