【线性基】hdu3949 XOR
给你n个数,问你将它们取任意多个异或起来以后,所能得到的第K小值?
求出线性基来以后,化成简化线性基,然后把K二进制拆分,第i位是1就取上第i小的简化线性基即可。注意:倘若原本的n个数两两线性无关,也即线性基的大小恰好为n时,异或不出零,否则能异或出零,要让K减去1。
这也是线性基的模板。
#include<cstdio>
#include<cstring>
using namespace std;
typedef long long ll;
ll d[64],p[64];
int cnt;//简化线性基的大小
bool Insert(ll val){//尝试插入线性基,返回是否插入成功
for(int i=62;i>=0;--i){
if(val&(1ll<<i)){
if(!d[i]){
d[i]=val;
break;
}
val^=d[i];
}
}
return val>0;
}
ll QueryMax(){
ll res=0;
for(int i=62;i>=0;--i){
if((res^d[i])>res){
res^=d[i];
}
}
return res;
}
ll QueryMin(){
for(int i=0;i<=62;++i){
if(d[i]){
return d[i];
}
}
return 0;
}
void Rebuild(){//化为简化线性基
for(int i=62;i>=0;--i){
for(int j=i-1;j>=0;--j){
if(d[i]&(1ll<<j)){
d[i]^=d[j];
}
}
}
for(int i=0;i<=60;++i){
if(d[i]){
p[cnt++]=d[i];
}
}
}
int T,n,m;
ll Kth(ll K){
if(cnt<n){
--K;//如果并非原本的n个数都线性无关的话,那么是能异或出零的
}
ll res=0;
if(K>=(1ll<<cnt)){
return -1ll;
}
for(int i=60;i>=0;--i){
if(K&(1ll<<i)){
res^=p[i];
}
}
return res;
}
int main(){
// freopen("hdu3949.in","r",stdin);
ll x;
scanf("%d",&T);
for(int zu=1;zu<=T;++zu){
memset(d,0,sizeof(d));
memset(p,0,sizeof(p));
cnt=0;
printf("Case #%d:\n",zu);
scanf("%d",&n);
for(int i=1;i<=n;++i){
scanf("%lld",&x);
Insert(x);
}
Rebuild();
scanf("%d",&m);
for(;m;--m){
scanf("%lld",&x);
printf("%lld\n",Kth(x));
}
}
return 0;
}
【线性基】hdu3949 XOR的更多相关文章
- [hdu3949]XOR(线性基求xor第k小)
题目大意:求xor所有值的第k小,线性基模板题. #include<cstdio> #include<cstring> #include<algorithm> #i ...
- LOJ.114.K大异或和(线性基)
题目链接 如何求线性基中第K小的异或和?好像不太好做. 如果我们在线性基内部Xor一下,使得从高到低位枚举时,选base[i]一定比不选base[i]大(存在base[i]). 这可以重构一下线性基, ...
- HDU3949 XOR (线性基)
HDU3949 XOR Problem Description XOR is a kind of bit operator, we define that as follow: for two bin ...
- HDU3949 XOR(线性基第k小)
Problem Description XOR is a kind of bit operator, we define that as follow: for two binary base num ...
- HDU3949:XOR(高斯消元)(线性基)
传送门 题意 给出n个数,任意个数任意数异或构成一个集合,询问第k大个数 分析 这题需要用到线性基,下面是一些资料 1.高斯消元&线性基&Matirx_Tree定理 笔记 2.关于线性 ...
- HDU3949/AcWing210 XOR (高斯消元求线性基)
求第k小的异或和,用高斯消元求更简单一些. 1 //用高斯消元求线性基 2 #include<bits/stdc++.h> 3 using namespace std; 4 #define ...
- Xor && 线性基练习
#include <cstdio> #include <cstring> ; ; int cnt,Ans,b,x,n; inline int Max(int x,int y) ...
- 【BZOJ-2115】Xor 线性基 + DFS
2115: [Wc2011] Xor Time Limit: 10 Sec Memory Limit: 259 MBSubmit: 2142 Solved: 893[Submit][Status] ...
- 【BZOJ-4269】再见Xor 高斯消元 + 线性基
4269: 再见Xor Time Limit: 10 Sec Memory Limit: 512 MBSubmit: 131 Solved: 81[Submit][Status][Discuss] ...
随机推荐
- 大聊Python----Select解析
1.首先列一下,sellect.poll.epoll三者的区别 select select最早于1983年出现在4.2BSD中,它通过一个select()系统调用来监视多个文件描述符的数组,当sele ...
- 【bug】vue-cli 3.0报错的解决办法
先上bug图片 bug说明:初装vue_cli3.0写了个组件,运行错误,显示如图, 代码提示:[Vue warn]: You are using the runtime-only build of ...
- Spring的BeanFactory体系结构(一)
本文使用的代码是: Spring 3.0 接 触Spring也有很长一段时间了.但是,每次都是直接使用Spring直接提供的API,时间久了,自然也会想探索Spring里面的奥秘.今天上 午,整理出了 ...
- js获取链接参数
var url = location.search; var Request = new Object(); if(url.indexOf("?")!=-1){ var str = ...
- Python标准库笔记(2) — re模块
re模块提供了一系列功能强大的正则表达式(regular expression)工具,它们允许你快速检查给定字符串是否与给定的模式匹配(match函数), 或者包含这个模式(search函数).正则表 ...
- python mysql插入数据遇到的错误
1.数据插入的时候报错:not enough arguments for format string,大概意思就是说没有足够的参数格式化字符串. 我的数据库插入方法是这样的 def add_data( ...
- 0x3F3F3F3F——ACM中的无穷大常量
在算法竞赛中,我们常常需要用到设置一个常量用来代表“无穷大”. 比如对于int类型的数,有的人会采用INT_MAX,即0x7fffffff作为无穷大.但是以INT_MAX为无穷大常常面临一个问题,即加 ...
- C# 笔记——索引器
索引器允许类或者结构的实例按照与数组相同的方式进行索引取值,索引器与属性类似,不同的是索引器的访问是带参的. 索引器和数组比较: (1)索引器的索引值(Index)类型不受限制 (2)索引器允许重载 ...
- rabbitmq在centos7下安装
知识预览 一. RabbitMQ队列 二. 事例 三.基于RabbitMQ的RPC 回到顶部 一. RabbitMQ队列 ? 1 2 3 4 5 #消息中间件 -消息队列 - 异步 提交的任务不需 ...
- python Mixin 是个啥?
内容待添加... 参考文章: [1][python] Mixin 扫盲班