题意:

给出\(n\)个数,求出子集异或第\(k\)小的值,不存在输出-1。

思路:

先用线性基存所有的子集,然后对线性基每一位进行消元,保证只有\(d[i]\)的\(i\)位存在1,那么这样变成了一组基线性基,然后按\(k\)的二进制找地k小。因为线性基不保存0,所以对有0的情况要进行特判。

代码:

#include<map>
#include<set>
#include<cmath>
#include<cstdio>
#include<stack>
#include<vector>
#include<queue>
#include<cstring>
#include<string>
#include<sstream>
#include<iostream>
#include<algorithm>
#define ll long long
using namespace std;
const int maxn = 1000 + 5;
const int INF = 0x3f3f3f3f;
const ll MOD = 1e9 + 7;
struct Liner_Basis{
ll d[63], nd[63];
int tot, flag;
void init(){
memset(d, 0, sizeof(d));
memset(nd, 0, sizeof(nd));
tot = flag = 0;
}
bool insert(ll x){
for(int i = 62; i >= 0; i--){
if(x & (1LL << i)){
if(d[i]) x ^= d[i];
else{
d[i] = x;
return true;
}
}
}
flag = 1;
return false;
}
void rebuild(){
for(int i = 62; i >= 0; i--){
if(d[i] == 0) continue;
for(int j = i - 1; j >= 0; j--){
if(d[i] & (1LL << j)) d[i] ^= d[j];
}
}
for(int i = 0; i <= 62; i++){
if(d[i]) nd[tot++] = d[i];
}
}
ll kth_min(ll k){
if(flag) k--;
if(k == 0) return 0;
if(k >= (1LL << tot)) return -1;
ll ret = 0;
for(int i = 62; i >= 0; i--){
if(k & (1LL << i)){
ret ^= nd[i];
}
}
return ret;
}
}lb;
int main(){
int T, ca = 1;
scanf("%d", &T);
while(T--){
int n;
ll x;
scanf("%d", &n);
lb.init();
for(int i = 1; i <= n; i++){
scanf("%lld", &x);
lb.insert(x);
}
lb.rebuild();
int q;
scanf("%d", &q);
printf("Case #%d:\n", ca++);
while(q--){
scanf("%lld", &x);
printf("%lld\n", lb.kth_min(x));
}
}
return 0;
}

HDU 3949 XOR (线性基第k小)题解的更多相关文章

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

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

  2. HDU 3949 XOR [线性基|高斯消元]

    目录 题目链接 题解 代码 题目链接 HDU 3949 XOR 题解 hdu3949XOR 搞死消元找到一组线性无关组 消出对角矩阵后 对于k二进制拆分 对于每列只有有一个1的,显然可以用k的二进制数 ...

  3. hdu 3949 XOR (线性基)

    链接: http://acm.hdu.edu.cn/showproblem.php?pid=3949 题意: 给出n个数,从中任意取几个数字异或,求第k小的异或和 思路: 线性基求第k小异或和,因为题 ...

  4. HDU 3949 XOR 线性基

    http://acm.hdu.edu.cn/showproblem.php?pid=3949 求异或第k小,结论是第k小就是 k二进制的第i位为1就把i位的线性基异或上去. 但是这道题和上一道线性基不 ...

  5. HDU3949 XOR(线性基第k小)

    Problem Description XOR is a kind of bit operator, we define that as follow: for two binary base num ...

  6. HDU 3949 XOR ——线形基 高斯消元

    [题目分析] 异或空间的K小值. 高斯消元和动态维护线形基两种方法都试了试. 动态维护更好些,也更快(QAQ,我要高斯消元有何用) 高斯消元可以用来开拓视野. 注意0和-1的情况 [代码] 高斯消元 ...

  7. HDU3949 XOR (线性基)

    HDU3949 XOR Problem Description XOR is a kind of bit operator, we define that as follow: for two bin ...

  8. HDU 3949 XOR(高斯消元搞基)

    HDU 3949 XOR pid=3949" target="_blank" style="">题目链接 题意:给定一些数字,问任取几个异或值第 ...

  9. HDU 3949 XOR [高斯消元XOR 线性基]

    3949冰上走 题意: 给你 N个数,从中取出若干个进行异或运算 , 求最后所有可以得到的异或结果中的第k小值 N个数高斯消元求出线性基后,设秩为$r$,那么总共可以组成$2^r$中数字(本题不能不选 ...

随机推荐

  1. Scrapy——將數據保存到MySQL數據庫

    Scrapy--將數據保存到MySQL數據庫 1. 在MySQL中創建數據庫表job_inf: 1 Create table job_inf( 2 id int(11) not null auto_i ...

  2. Jmeter-插件扩展及性能监控插件的安装

    需要对http服务进行大数据量的传值测试:看看产品中的http服务,能支持传多少字符:目标值是希望能到10w+: 上次测试中,服务器总是内存满导致服务不响应,因此想增加对服务端的性能监控:查阅了smi ...

  3. 支付宝沙箱环境使用(Alipay Easy SDK ) .Net示例

    新版服务端 SDK(Alipay Easy SDK)适用于 Java.C#.PHP 编程语言,对开放能力的 API 进行了更加贴近高频场景的精心设计与裁剪,简化了服务端调用方式,让开发者享受极简编程体 ...

  4. Vue基础之Vue组件

    Vue基础之Vue组件 // 组件是可以复用的Vue实例! // 可以把经常重复的功能封装为组件!

  5. navicat premium 11.0.17 破解版

    下载地址: 链接:https://pan.baidu.com/s/1zBoKRAaQZb2p2weelJpKMQ       提取码:b8dd 一款功能强大的数据库管理工具Navicat Premiu ...

  6. js12种应该注意的地方

    1. == Javascript有两组相等运算符,一组是==和!=,另一组是===和!==.前者只比较值的相等,后者除了值以外,还比较类型是否相同. 请尽量不要使用前一组,永远只使用===和!==.因 ...

  7. ValueError: the environment variable is longer than 32767 characters On Windows, an environment variable string ("name=value" string) is limited to 32,767 characters

    https://github.com/python/cpython/blob/aa1b8a168d8b8dc1dfc426364b7b664501302958/Lib/test/test_os.py ...

  8. 容器调度 • Docker网络 • 持续交付 • 动态运行应用程序 部署的多元化

    <英雄联盟>在线服务运维之道 - InfoQ https://www.infoq.cn/article/running-online-services-riot/ 第一章 简 介 我是Jo ...

  9. redis list 列表 查找 时间复杂度

    http://redisbook.com/preview/intset/content.html 列表对象 列表对象的编码可以是 ziplist 或者 linkedlist . ziplistFind ...

  10. Redis 雪崩、穿透和击穿

    https://github.com/doocs/advanced-java/blob/master/docs/high-concurrency/redis-caching-avalanche-and ...