题意:

给出\(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. 入门OJ:photo

    题目描述 有N个人,来自K个家族.他们排成一行准备照相,但是由于天生的排外性,每个人都希望和本家族的人站在一起,中间不要加入别的家族的人.问最少从队列中去掉多少个就可以达到这个目的. 输入格式 第一行 ...

  2. TCP客户端程序

    TCP客户端程序的函数调用顺序为:socket -> connect -> send/recv socket.send和recv函数在TCP服务器程序中已经说过了,这里就不赘述了. con ...

  3. 从零搭建一个IdentityServer——项目搭建

    本篇文章是基于ASP.NET CORE 5.0以及IdentityServer4的IdentityServer搭建,为什么要从零搭建呢?IdentityServer4本身就有很多模板可以直接生成一个可 ...

  4. 第一个 Maven 应用程序

    概述 使用 Maven 创建一个 Java Web 应用程序 创建 Maven 项目 选择 File -> New -> Project... 选择 Maven 项目 填写项目信息 选择工 ...

  5. (008)每日SQL学习:Oracle Not Exists 及 Not In 使用

    今天遇到一个问题,not in 查询失效,我以为是穿越了,仔细查了点资料,原来理解有误! select value from temp_a a where a.id between 1 and 100 ...

  6. (Sql Server)存储过程(转载)

    SQL Server 存储过程 Transact-SQL中的存储过程,非常类似于Java语言中的方法,它可以重复调用.当存储过程执行一次后,可以将语句缓存中,这样下次执行的时候直接使用缓存中的语句.这 ...

  7. 类型检查 Type Checking 一些编程语言并不安全 类型化语言的优点 定型环境 (符号表) 断言的种类

    Compiler http://staff.ustc.edu.cn/~bjhua/courses/compiler/2014/ http://staff.ustc.edu.cn/~bjhua/cour ...

  8. 正则r的作用

    >>> mm = "c:\\a\\b\\c" >>> mm 'c:\\a\\b\\c' >>> print(mm) c:\a\ ...

  9. null调整为not null default xxx,不得不注意的坑

    最近碰到一个case,值得分享一下. 现象 一个DDL,将列的属性从null调整为not null default xxx, alter table slowtech.t1 modify name v ...

  10. Spring Boot,Spring Cloud,Eureka,Actuator,Spring Boot Admin,Stream,Hystrix

    Spring Boot,Spring Cloud,Eureka,Actuator,Spring Boot Admin,Stream,Hystrix 一.Spring Cloud 之 Eureka. 1 ...