题目链接: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
2011 Multi-University Training Contest 11 - Host by UESTC
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+7;
const int maxn=1e4+10;
const int maxk=5e3+10;
const int maxx=1e4+10;
const ll maxe=1000+10;
#define INF 0x3f3f3f3f3f3f
#define Lson l,mid,rt<<1
#define Rson mid+1,r,rt<<1|1
ll a[maxn],b[100];
ll m,k;
void guass(int n)
{memset(b,0,sizeof(b));for(int i=0;i<n;i++){for(int j=63;j>=0;j--){if((a[i]>>j)&1){if(!b[j]){b[j]=a[i];break;}else{a[i]^=b[j];}}}}for(int i=63;i>=0;i--){if(!b[i]) continue;for(int j=i+1;j<=63;j++){if((b[j]>>i)&1) b[j]^=b[i];//为了使得只有b[i]的第i为1,其它的都不为1
        }}m=0;for(int i=0;i<=63;i++) if(b[i]) b[m++]=b[i];
}
int main()
{int t,ca=1,n,q;cin>>t;while(t--){cin>>n;printf("Case #%d:\n",ca++);for(int i=0;i<n;i++){cin>>a[i];}guass(n);cin>>q;while(q--){ll ans=0;cin>>k;if(n!=m) k--;//代表可以是0if(k>=(1ll<<m)) cout<<"-1"<<endl;else{for(int i=0;i<=63;i++) if((k>>i)&1) ans^=b[i];cout<<ans<<endl;}}}return 0;
}

转载于:https://www.cnblogs.com/caijiaming/p/9637189.html

hdu3949(线性基,求第k小的异或和相关推荐

  1. 借组磁带机求第K小元素

    如果输入在磁带机上, 你的机器只有一个磁带机驱动器和几十字的内存,如何找第K小的数 1. 遍历一遍磁带,随即选择一个数M 2. 再遍历一遍磁带, 计算大于和小于M的个数,这样就可以获得数M在总序列中的 ...

  2. 【最详细】BFPRT算法:时间复杂度O(n)求第k小的数字

    去年写了一篇对快排进行改进的算法,可以在时间复杂度 O(n)O(n)O(n)的情况下,找到第kkk小的数字. 那时候,我还不知道这个算法叫BFPRT算法--现在知道了,还知道它又被称为中位数的中位数算 ...

  3. 2019牛客多校第四场 B xor (线性基求交)

    xor 思路 题目是要求[l,r][l, r][l,r]的所有集合是否都可以得到xxx,那么显然我们可以对这[l,r][l, r][l,r]个线性基求交,然后再特判能否xxx能否插入,如果能插入,显然 ...

  4. 分治算法 求第k小元素 O(n) O(nlog2^n)

    BFPRT算法:时间复杂度O(n)求第k小的数字(分治算法+快排) 各位小伙伴,由于本篇文章代码太过杂乱.我于 2018年12月25日 对文中介绍的算法进行了重写.点击上面的蓝色字体,可以阅读重写后的 ...

  5. BFPRT算法:时间复杂度O(n)求第k小的数字(分治算法+快排)

    我自己搭建了博客,以后可能不太在CSDN上发博文了,https://www.qingdujun.com/ . 去年写了一篇<分治算法 求第kkk小元素 O(n)O(n)O(n) & O( ...

  6. 两个有序数组合起来求第k小的数+左老师专访ACM大神(笔记)8月5日斗鱼直播实录

    1.长度相等的两个有序数组寻找上中位数 注:上中位数1 2 3 4 5 6为3(偶数两个中位数为前面那个) 思路:去掉不可能为上中位数的,剩下的简化组合求上中位数. 1.1 奇数序列 位置 位置 位置 ...

  7. 线性时间选择求第k小数(分治)

    目录 线性时间选择 优化 对划分进行随机数改良 取中位数进行划分 全部代码 线性时间选择 问题描述:给定线性序集中 n 个元素和一个整数 k(1 <= k <= n),要求找出着 n 个元 ...

  8. 求第k小元素:采用特定分治策略

    问题[描述算法问题,首选形式化方式(数学语言),其次才是非形式化方式(日常语言)]设L是n个元素的集合,从L中选取第k小的元素,其中1<=k<=n.这里的第k小元素是指,当L按从小到大排好 ...

  9. POJ 1442 Black Box(大小堆,求第K小的元素)

    文章目录 1. 题目链接 2. 题目解读 3. 代码 3.1 Runtime Error 代码 1. 题目链接 http://poj.org/problem?id=1442 2. 题目解读 可以利用大 ...

最新文章

  1. vcm驱动芯片原理_T6322A|电源芯片的内部设计是怎样的?
  2. Android--框架布局
  3. C++编程-预定义宏
  4. (第六场)Singing Contest 【模拟】
  5. 人工神经网络_验证码破译(数据挖掘入门与实践-实验9)
  6. php 精度运算,PHP BC 库(任意精度数字运算) | 网游世界
  7. linux下dns服务器安装,Linux下DNS服务器安装配置方法详细介绍
  8. 官方科普iQOO 5 120W闪充方案:首发6C高倍率电芯 15分钟充入100%
  9. INPUT只能输入数字
  10. 中山纪念中学培训DAY1
  11. 【VerySky原创】怎么查找系统中的锁对象
  12. 使用 ExpandableListView 实现折叠ListView
  13. SpreadJS V14.2.0 放假前Crack
  14. 栈和队列_入门oj题目练习:1用队列实现栈2用栈实现队列
  15. 设计师经常逛的色彩搭配网站—配色方案吧
  16. matlab产生窄带信号,窄带信号
  17. mysql 获取当天0点 和 当天23点59分59秒
  18. nyoj54小明的存钱计划
  19. 关于ThinkPad T490s 风扇不转动问题
  20. 利用telemetry进行权限维持

热门文章

  1. 关于 error: LNK1123: failure during conversion to COFF: file invalid or corrupt 错误的解决方案【Qt】【 VS2010】
  2. TOMCAT启动汉字乱码解决方案
  3. IDEA(2018)连接MySQL数据库失败的解决方法(报错08001)
  4. 【实践】面向广告主的猜你喜欢推荐与B端用户增长实践.pdf(附下载链接)
  5. 【实践】万字干货:如何优雅地记录操作日志?(附代码)
  6. 【报告分享】2020金融兴趣人群内容消费及理财意识洞察报告.pdf(附下载链接)...
  7. classification、part segmentation、semantic segmentation、instance segmentation
  8. 图嵌入表示TADW:当DeepWalk加上外部文本信息
  9. 开发物体识别桌、_Tofu3 热红外可见光双光AI目标识别跟踪
  10. Facebook广告营销的6个方法经验分享