线性基求第k小异或值
题目链接
题意:给由 n 个数组成的一个可重集 S,每次给定一个数 k,求一个集合 \(T \subseteq S\),
使得集合 T 在 S 的所有非空子集的不同的异或和中,
其异或和 \(T_1 \mathbin{\text{xor}} T_2 \mathbin{\text{xor}} \ldots \mathbin{\text{xor}}T_{|T|}\)是第 k 小的。
/*
1.照例建立线性基
2.使得线性基中有且只有base[i]的第i位为1
3.记录所有有值的base[] 从低位到高位记为0~cnt,共cnt + 1个 (注:闭区间
这时线性基可以构成的数有(1 << cnt) + 1个,如果cnt + 1 < n的话 说明可以取零 这时可以构成的数有(1 << (cnt + 1))个
4.取 第k小 时
如果k大于可以构成的数的总数 那么无解
否则res是所有base[i] ((k - 1)的第i位为1) 的异或和
*/
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
using namespace std;
const int N = 55;
int n, m;
struct BASE{
long long w[N];
int cnt;
void init(){
memset(w, 0, sizeof(w));
}
void ins(long long x){
for(int i = 50; i >= 0; --i){
if((x >> i) & 1)
if(w[i]) x ^= w[i];
else {w[i] = x; break;}
}
}
void build(){
for(int i = 50; i >= 0; --i){
if(!w[i]) continue;
for(int j = i + 1; j <= 50; ++j){
if((w[j] >> i) & 1){
w[j] ^= w[i];
}
}
}
//for(int i = 0; i <= 5; ++i) printf("%d %lld\n", i, w[i]);
for(int i = 0; i <= 50; ++i){
//printf("%d %lld\n", i, w[i]);
if(w[i]){
w[cnt++] = w[i];
// printf("%d %lld\n", cnt - 1, w[cnt - 1]);
}
}
--cnt;
}
long long query(long long x){
long long res = 0;
for(int i = cnt; i >= 0; --i){
if((x >> i) & 1) res ^= w[i];
}
return res;
}
}base;
int main() {
base.init();
scanf("%d", &n);
long long x;
for(int i = 1; i <= n; ++i){
scanf("%lld", &x);
base.ins(x);
}
base.build();
scanf("%d", &m);
for(int i = 1; i <= m; ++i){
scanf("%lld", &x);
if(n != base.cnt + 1) --x;//注意是非空子集 所以特判可否取零
if(x >= (1ll << (base.cnt + 1))) printf("-1\n"); //这种情况下无法取到
else printf("%lld\n", base.query(x));
}
return 0;
}
线性基求第k小异或值的更多相关文章
- hdu3949(线性基,求第k小的异或和
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3949 XOR Time Limit: 2000/1000 MS (Java/Others) Me ...
- 算法导论学习之线性时间求第k小元素+堆思想求前k大元素
对于曾经,假设要我求第k小元素.或者是求前k大元素,我可能会将元素先排序,然后就直接求出来了,可是如今有了更好的思路. 一.线性时间内求第k小元素 这个算法又是一个基于分治思想的算法. 其详细的分治思 ...
- hdu 3949 XOR 线性基 第k小异或和
题目链接 题意 给定\(n\)个数,对其每一个子集计算异或和,求第\(k\)小的异或和. 思路 先求得线性基. 同上题,转化为求其线性基的子集的第k小异或和. 结论 记\(n\)个数的线性基为向量组\ ...
- 树状数组求第k小的元素
int find_kth(int k) { int ans = 0,cnt = 0; for (int i = 20;i >= 0;i--) //这里的20适当的取值,与MAX_VAL有关,一般 ...
- hdu 4217 Data Structure? 树状数组求第K小
Data Structure? Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) ...
- 线性基求交(2019牛客国庆集训派对day4)
题意:https://ac.nowcoder.com/acm/contest/1109/C 问你有几个x满足A,B集合都能XOR出x. 思路: 就是线性基求交后,有几个基就是2^几次方. #defin ...
- 求第k小的数
题目链接:第k个数 题意:求n个数中第k小的数 题解: //由快速排序算法演变而来的快速选择算法 #include<iostream> using namespace std; const ...
- 两个有序数组中的中位数以及求第k个最小数的值
解法参考 <[分步详解]两个有序数组中的中位数和Top K问题> https://blog.csdn.net/hk2291976/article/details/51107778 里面求中 ...
- [hdu3949]XOR(线性基求xor第k小)
题目大意:求xor所有值的第k小,线性基模板题. #include<cstdio> #include<cstring> #include<algorithm> #i ...
随机推荐
- from bs4 import BeautifulSoup 报错
一: BeautifulSoup的安装: 下载地址:https://www.crummy.com/software/BeautifulSoup/bs4/download/4.6/ 下载后,解压缩,然后 ...
- long long 的输入输出问题
参考博客:https://www.byvoid.com/zhs/blog/c-int64
- 通过C#调用,实现js加密代码的反混淆,并运行js函数
前一篇我测试了vba调用htmlfile做反混淆,并执行js加密函数的代码.本文换成C#实现. 联系QQ:564955427 C#操作JS函数,可以通过ScriptControl组件,但这个组件只能在 ...
- Elasticsearch的DSL之比较重要的几个查询语句
1. match_all { "match_all": {}} 匹配所有的, 当不给查询条件时,默认. 2. match 进行full text search或者exact va ...
- Java工具类——UUIDUtils
借用一下百度百科的解释,来看一下UUID是什么. UUID含义是通用唯一识别码 (Universally Unique Identifier),这 是一个软件建构的标准,也是被开源软件基金会 (Ope ...
- node-cookie-parserDemo
let express = require('express'); let app = new express(); let cookieParser = require('cookie-parser ...
- eclipse打开package explorer视图
第一步:window-show view-other 第二步:
- python学习笔记(10)--组合数据类型(序列类型)
序列是具有先后关系的一组数据,是一维元素向量,元素类型可以不同,类似数学元素序列,元素间由序号引导,通过下标访问序列的特定元素.序列类型是一个基类类型,字符串类型,元祖类型,列表类型都属于序列类型. ...
- cmd & tree & bash
cmd & tree & bash bug E: Unable to locate package tree solution # 1. update $ sudo apt-get u ...
- Vue.js文档
参考网址:https://vuefe.cn/ 第一 安装 1.下载到本地后使用<script>标签直接引入 2.使用CDN引入 例如:使用CDN引入 <script src=&qu ...