题目链接

题意:给由 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小异或值的更多相关文章

  1. hdu3949(线性基,求第k小的异或和

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3949 XOR Time Limit: 2000/1000 MS (Java/Others)    Me ...

  2. 算法导论学习之线性时间求第k小元素+堆思想求前k大元素

    对于曾经,假设要我求第k小元素.或者是求前k大元素,我可能会将元素先排序,然后就直接求出来了,可是如今有了更好的思路. 一.线性时间内求第k小元素 这个算法又是一个基于分治思想的算法. 其详细的分治思 ...

  3. hdu 3949 XOR 线性基 第k小异或和

    题目链接 题意 给定\(n\)个数,对其每一个子集计算异或和,求第\(k\)小的异或和. 思路 先求得线性基. 同上题,转化为求其线性基的子集的第k小异或和. 结论 记\(n\)个数的线性基为向量组\ ...

  4. 树状数组求第k小的元素

    int find_kth(int k) { int ans = 0,cnt = 0; for (int i = 20;i >= 0;i--) //这里的20适当的取值,与MAX_VAL有关,一般 ...

  5. hdu 4217 Data Structure? 树状数组求第K小

    Data Structure? Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) ...

  6. 线性基求交(2019牛客国庆集训派对day4)

    题意:https://ac.nowcoder.com/acm/contest/1109/C 问你有几个x满足A,B集合都能XOR出x. 思路: 就是线性基求交后,有几个基就是2^几次方. #defin ...

  7. 求第k小的数

    题目链接:第k个数 题意:求n个数中第k小的数 题解: //由快速排序算法演变而来的快速选择算法 #include<iostream> using namespace std; const ...

  8. 两个有序数组中的中位数以及求第k个最小数的值

    解法参考 <[分步详解]两个有序数组中的中位数和Top K问题> https://blog.csdn.net/hk2291976/article/details/51107778 里面求中 ...

  9. [hdu3949]XOR(线性基求xor第k小)

    题目大意:求xor所有值的第k小,线性基模板题. #include<cstdio> #include<cstring> #include<algorithm> #i ...

随机推荐

  1. DOM节点左右移动

    闲来没事写了个小demo,原本是回答别人博问的,有人比我更快的给出了链接,想想半途而废也不好,就写完了,写个博文记录一下(效果是按照我自己来的,可能和最早别人问的不太一样,反正无关紧要啦) 直接上co ...

  2. HTML,CSS笔记

    text-indent 属性规定文本块中首行文本的缩进.允许使用负值.如果使用负值,那么首行会被缩进到左边.p{ text-indent:50px; } HTML <label> 标签的 ...

  3. IdentityServer4【QuickStart】之使用asp.net core Identity

    使用asp.net core Identity IdentityServer灵活的设计中有一部分是可以将你的用户和他们的数据保存到数据库中的.如果你以一个新的用户数据库开始,那么,asp.net co ...

  4. C#中闭包的陷阱

    我们在使用lambda的时候会遇到闭包,在闭包中有一个陷阱是在for循环中产生的,先上代码: class Program { static void Main(string[] args) { Act ...

  5. [转帖]SSH 的 三种代理功能.

    https://www.zhihu.com/people/skywind3000/activities 之前用过frp 知道 ssh 有一 反向代理的功能 没想到 还有这么多高级功能 收藏一下. ss ...

  6. kubernetes常用命令

    #.查询信息 kubectl get [需要查询的服务]   node 节点componentstatuses 简写 cs 组件状态namespaces 简写 ns 名命空间pod pod信息 添加  ...

  7. 浅谈WPF的VisualBrush

    首先看看VisualBrush的解释,msdn上面的解释是使用 Visual 绘制区域,那么我们再来看看什么是Visual呢?官方的解释是:获取或设置画笔的内容,Visual 是直接继承自Depend ...

  8. Delphi调用MSSQL存储过程返回的多个数据集的方法

    varaintf:_Recordset;RecordsAffected:OleVariant; begin ADOStoredProc1.Close;ADOStoredProc1.Open;aintf ...

  9. orecal基本连接数据库简介

    整理自互联网 一. jdbc:oracle:thin:@192.168.3.98:1521:orcljdbc:表示采用jdbc方式连接数据库oracle:表示连接的是oracle数据库thin:表示连 ...

  10. commons-lang

    今天在编码的过程中,对于null,采用==null进行判断.并且为了过滤"",使用了str.trim().length()==0,当str为null时,报空指针异常. 于是决定使用 ...