HDU3949:XOR——题解
http://acm.hdu.edu.cn/showproblem.php?pid=3949
求n个数的异或和第k小。
参考:https://blog.sengxian.com/algorithms/linear-basis
没了。
#include<cstdio>
#include<iostream>
#include<cstring>
#include<vector>
#include<algorithm>
using namespace std;
typedef long long ll;
const int BASE=;
ll b[];
int n,cnt;
vector<ll>mp;
ll query(ll k){
if(cnt!=n)k--;
if(k>(1LL<<(int)mp.size())-)return -;
ll ans=;
for(int i=;i<(int)mp.size();i++){
if(k>>i&)ans^=mp[i];
}
return ans;
}
inline void init(){
cnt=;
memset(b,,sizeof(b));
mp.clear();
}
int main(){
int t;
scanf("%d",&t);
for(int cas=;cas<=t;cas++){
printf("Case #%d:\n",cas);
scanf("%d",&n);
init();
for(int i=;i<=n;i++){
ll a;scanf("%lld",&a);
for(int j=BASE;j>=;j--){
if(a>>j&){
if(b[j])a^=b[j];
else{
b[j]=a;cnt++;
for(int k=j-;k>=;k--)
if(b[k]&&(b[j]>>k&))b[j]^=b[k];
for(int k=j+;k<=BASE;k++)
if(b[k]>>j&)b[k]^=b[j];
break;
}
}
}
}
for(int i=;i<=BASE;i++)
if(b[i])mp.push_back(b[i]);
int q;
scanf("%d",&q);
while(q--){
ll k;
scanf("%lld",&k);
printf("%lld\n",query(k));
}
}
return ;
}
+++++++++++++++++++++++++++++++++++++++++++
+本文作者:luyouqi233。 +
+欢迎访问我的博客:http://www.cnblogs.com/luyouqi233/+
+++++++++++++++++++++++++++++++++++++++++++
HDU3949:XOR——题解的更多相关文章
- HDU3949 XOR (线性基)
HDU3949 XOR Problem Description XOR is a kind of bit operator, we define that as follow: for two bin ...
- hdu3949 XOR xor高斯消元
XOR Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...
- 【线性基】hdu3949 XOR
给你n个数,问你将它们取任意多个异或起来以后,所能得到的第K小值? 求出线性基来以后,化成简化线性基,然后把K二进制拆分,第i位是1就取上第i小的简化线性基即可.注意:倘若原本的n个数两两线性无关,也 ...
- [hdu3949]XOR(线性基求xor第k小)
题目大意:求xor所有值的第k小,线性基模板题. #include<cstdio> #include<cstring> #include<algorithm> #i ...
- HDU3949 XOR(线性基第k小)
Problem Description XOR is a kind of bit operator, we define that as follow: for two binary base num ...
- HDU3949 XOR
嘟嘟嘟 集训的时候发现自己不会线性基,就打算学一下. 这东西学了挺长时间,其实不是因为难,而是天天上午考试,下午讲题,结果晚上就开始颓了. 今天总算是有大块的时间好好学了一遍. 这里推荐menci大佬 ...
- BZOJ2115:[WC2011]Xor——题解
https://www.lydsy.com/JudgeOnline/problem.php?id=2115 https://www.luogu.org/problemnew/show/P4151 这道 ...
- HDU3949:XOR(高斯消元)(线性基)
传送门 题意 给出n个数,任意个数任意数异或构成一个集合,询问第k大个数 分析 这题需要用到线性基,下面是一些资料 1.高斯消元&线性基&Matirx_Tree定理 笔记 2.关于线性 ...
- 【HDU3949】XOR
[题目大意] 给定一个数组,求这些数组通过异或能得到的数中的第k小是多少. 传送门:http://vjudge.net/problem/HDU-3949 [题解] 首先高斯消元求出线性基,然后将k按照 ...
随机推荐
- java中i=i++的解析
int i = 0; i = i++; //答案是0 System.out.println(i); 执行以上代码,奇怪的是打印出来i的结果是0,说简单点,i++是一个表达式,是有返回值的,返回的是自增 ...
- TPO-11 C1 Use the gym pass
TPO-11 C1 Use the gym pass 第 1 段 1.Listen to a conversation between a student and a university emplo ...
- 372. Delete Node in a Linked List【LintCode java】
Description Implement an algorithm to delete a node in the middle of a singly linked list, given onl ...
- 【zabbix 监控】第二章 安装测试被监控主机
客户端安装测试 一.准备两台被监控主机,分别做如下操作: web129:192.168.19.129 web130:192.168.19.130 [root@web129 ~]#yum -y inst ...
- 论文笔记:Deep Attentive Tracking via Reciprocative Learning
Deep Attentive Tracking via Reciprocative Learning NIPS18_tracking Type:Tracking-By-Detection 本篇论文地主 ...
- Docker 镜像构建的时候,应该小心的坑
不要改文件 如果run了以后,你还需要进入到容器中,修改容器的配置,那么,这个容器是危险的.一旦容器无法启动,就再也改不了配置.那么你就需要删除和重新run这个容器,而配置要再改一遍.一个可用的镜像在 ...
- Python3 Tkinter-Toplevel
1.创建 Toplevel与Frame类似,但是它包含窗体属性(如Title) from tkinter import * root=Tk() tl=Toplevel() Label(tl,text= ...
- [C++] Fucntions
Statements A break statements terminate the nearest wile, do while, for or switch statement. A break ...
- 第一次接触FPGA至今,总结的宝贵经验
从大学时代第一次接触FPGA至今已有10多年的时间,至今记得当初第一次在EDA实验平台上完成数字秒表.抢答器.密码锁等实验时那个兴奋劲.当时由于没有接触到HDL硬件描述语言,设计都是在MAX+plus ...
- C语言 内存分配 地址 指针 数组 参数 实例解析
. Android源码看的鸭梨大啊, 补一下C语言基础 ... . 作者 : 万境绝尘 转载请注明出处 : http://blog.csdn.net/shulianghan/article/detai ...