线性基入门

今天学习了神奇的线性基,主要是在解决异或问题时比较有用。
详细的解释和证明有大佬珠玉在前,如果感兴趣可以移步
补充一下自己的理解:
可以联系线性代数极大无关组进行理解,线性基就相当于异或的向量空间中的极大无关组(线性代数中的是加减的向量空间,因此线性基有极大无关组的性质:

  • 向量的个数一定
  • 可以表示向量空间内任意一个向量
  • 向量空间中不包括零向量

但是我们在计算线性基的时候为什么要从最高位开始选呢?这其实只是为了方便使用,相当于我们在求极大无关组的时候习惯于从左往右进行初等变换的习惯一样。同样的,我们也可以从低位开始选,也是不会影响的,可是这样选出来不方便我们使用(从高位到低位方便求最大值最小值)
在求第K大的时候我们还需要对线性基进行重建,这相当将行阶梯形阵变换为行最简形矩阵一样。
这样我们就可以从大往小依次找出第k大。可能会疑惑为什么不考虑后面的1的影响只考虑最高位1的影响就可以呢?因为高位的1的影响更大,高位有1没1的影响大于后面那些数字的影响,所以只考虑高位的影响就可以。

这里给出模板题的代码。
【题目描述】
XOR is a kind of bit operator, we define that as follow: for two binary base number A and B, let C=A XOR B, then for each bit of C, we can get its value by check the digit of corresponding position in A and B. And for each digit, 1 XOR 1 = 0, 1 XOR 0 = 1, 0 XOR 1 = 1, 0 XOR 0 = 0. And we simply write this operator as ^, like 3 ^ 1 = 2,4 ^ 3 = 7. XOR is an amazing operator and this is a question about XOR. We can choose several numbers and do XOR operatorion to them one by one, then we get another number. For example, if we choose 2,3 and 4, we can get 234=5. Now, you are given N numbers, and you can choose some of them(even a single number) to do XOR on them, and you can get many different numbers. Now I want you tell me which number is the K-th smallest number among them.
Input
First line of the input is a single integer T(T<=30), indicates there are T test cases.
For each test case, the first line is an integer N(1<=N<=10000), the number of numbers below. The second line contains N integers (each number is between 1 and 10^18). The third line is a number Q(1<=Q<=10000), the number of queries. The fourth line contains Q numbers(each number is between 1 and 10^18) K1,K2,…KQ.
Output
For each test case,first output Case #C: in a single line,C means the number of the test case which is from 1 to T. Then for each query, you should output a single line contains the Ki-th smallest number in them, if there are less than Ki different numbers, output -1.
Sample Input

2
2
1 2
4
1 2 3 4
3
1 2 3
5
1 2 3 4 5

Sample Output

Case #1:
1
2
3
-1
Case #2:
0
1
2
3
-1

【AC代码】

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<set>
#include<climits>
#include<cstdlib>
#include<cmath>using namespace std;typedef long long ll;const int MAXN=10005;
int n,q;
ll k,tmp;
struct L_B
{ll b[65],p[65];int cnt,flag;L_B(){memset(p,0,sizeof(p));memset(b,0,sizeof(b));cnt=flag=0;}inline bool insert(ll x){for(int i=62;i>=0;--i)if(x&(1ll<<i)){if(b[i])x^=b[i];else{b[i]=x;return true;}}flag=1;return false;}ll get_max(){ll ret = 0;for(int i=62;i>=0;--i)if((ret^b[i])>ret)ret^=b[i];return ret;}ll get_min(){if(flag)return 0;for(int i=0;i<=62;++i)if(b[i])return b[i];return 0;}inline void rebuild(){for(int i = 1;i <= 62;++i)if(b[i])for(int j=0;j<i;++j)if(b[i]&(1ll<<j))b[i]^=b[j];for(int i=0;i<=62;++i)if(b[i])p[cnt++]=b[i];}ll kth(ll k){if(flag)--k;if(k==0)return 0;ll ret = 0;if(k>=(1ll<<cnt))return -1;for(int i=0;i<=cnt-1;++i)if(k&(1ll<<i))ret^=p[i];return ret;}
};
/*
L_B merge(const L_B &n1,const L_B &n2)
{L_B ret = n1;for(int i = 0;i <= 62;++i)if(n2.b[i])ret.insert(n2.b[i]);ret.flag = n1.flag | n1.flag;return ret;
}
*/
int main()
{int T;scanf("%d",&T);for(int Case=1;Case<=T;++Case){L_B lis;scanf("%d",&n);for(int i = 1;i <= n;++i){scanf("%lld",&tmp);lis.insert(tmp);}scanf("%d",&q);lis.rebuild();printf("Case #%d:\n",Case);while(q--){scanf("%lld",&k);	printf("%lld\n",lis.kth(k));}}return 0;
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/384106.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

单例模式及C++实现代码

http://www.cnblogs.com/cxjchen/p/3148582.html 单例模式 单例模式&#xff0c;可以说设计模式中最常应用的一种模式了&#xff0c;据说也是面试官最喜欢的题目。但是如果没有学过设计模式的人&#xff0c;可能不会想到要去应用单例模式&#xff0c;面对单例模式适用的情况&am…

UVALive - 8512——线段树维护线性基

【题目描述】 UVALive - 8512XOR 【题目分析】 这种区间线性基的问题我们可以考虑用线段树维护&#xff0c;线性基的合并的话就直接暴力合并 找到所在区间的线性基后再查找最大的数&#xff0c;我看网上的博客要说消除k的影响什么的&#xff0c;我觉得没有什么必要&#xff0c;…

命名管道

1.命名管道的创建 (1) 通过命令创建 mkfifo filename (2)在程序中创建 int mkfifo(const char* filename, mode_t mode); 2. 命名管道和匿名管道的区别 (1)匿名管道由pipe函数创建并且打开     (2)命名管道有mkfifo函数创建由open函数打开     (3) fifo 之间的两…

HYSBZ - 1101——莫比乌斯反演

【题目描述】 HYSBZ - 1101 【题目分析】 昨天测试出了一道差不多的题目&#xff0c;我只能想到暴力&#xff0c;各种优化&#xff0c;最后都是运行了好久TLE&#xff0c;最后才知道要用到莫比乌斯反演&#xff0c;就想着今天研究一下&#xff0c;得出的结论就是&#xff0c;我…

Linux下I/O多路转接之select --fd_set

http://blog.csdn.net/li_ning_/article/details/52165993 fd_set 你终于还是来了&#xff0c;能看到这个标题进来的&#xff0c;我想&#xff0c;你一定是和我遇到了一样的问题&#xff0c;一样的疑惑&#xff0c;接下来几个小时&#xff0c;我一定竭尽全力&#xff0c;写出我…

BZOJ 2844 | HYSBZ - 2844albus就是要第一个出场——线性基

【题目描述】 BZOJ 2844 | HYSBZ - 2844albus 【题目分析】 题目的意思大概是给一个数列&#xff0c;他有2n个子集&#xff0c;每个子集的元素的异或和构成新的一个数列&#xff0c;排序后问数字Q在这个序列里面的下标。 假如题目是求所有元素的异或和构成一个集合就好弄了&…

CodeForces - 641ELittle Artem and Time Machine——map+树状数组

【题目描述】 CodeForces - 641ELittle Artem and Time Machine 【题目分析】 题目的意思大概是有三种操作 1.在时间t加入一个数字x 2.在时间t删除一个数字x 3.询问在时间t集合里面x的个数 虽然题目描述很简单&#xff0c;但是t和x的范围都是109&#xff0c;我一开始想到的是主…

I/O多路转接之poll 函数

http://blog.csdn.net/li_ning_/article/details/52167224 poll 一、poll()函数&#xff1a; 这个函数是某些Unix系统提供的用于执行与select()函数同等功能的函数&#xff0c;自认为poll和select大同小异&#xff0c;下面是这个函数的声明&#xff1a; [cpp] view plaincopy …

链表相关笔试面试题

1.判断两个链表是否相交 两个链表是否相交可分为以下几种情况     &#xff08;1&#xff09;两个链表都不带环&#xff0c;此时两个链表所对应的最后一个节点是相等的     &#xff08;2&#xff09;两个链表一个带环&#xff0c;一个不带环&#xff0c;两个链表一定…

Linux经典问题—五哲学家就餐问题

http://m.blog.csdn.net/aspenstars/article/details/70149038 一、问题介绍 由Dijkstra提出并解决的哲学家进餐问题(The Dinning Philosophers Problem)是典型的同步问题。该问题是描述有五个哲学家共用一张圆桌&#xff0c;分别坐在周围的五张椅子上&#xff0c;在圆桌上有五…

修改之前的myshell使之支持输入输出重定向

1.open函数     这个函数是打开一个文件&#xff08;文件名叫pathname),以 flag 权限打开&#xff0c;flag 包括了以下几种 O_RDONLY&#xff08;只读&#xff09;, O_WRONLY&#xff08;只写&#xff09;, O_RDWR&#xff08;读写&#xff09;&#xff0c;当文件打开成…

HDU - 6621 K-th Closest Distance——主席树+二分

【题目描述】 HDU - 6621 K-th Closest Distance 【题目分析】 因为看到第kkk大的要求&#xff0c;刚开始的时候一直都在想怎么运用第kkk大来解决问题&#xff0c;但是后来看其他人的博客才发现并不需要用第k大&#xff0c;但是主席树维护权值线段树还是需要的&#xff0c;这…

链表相关的算法题大汇总 — 数据结构之链表奇思妙想

http://blog.csdn.net/lanxuezaipiao/article/details/22100021基本函数&#xff08;具体代码实现见后面&#xff09; 1&#xff0c;构造节点 //定义节点类型 struct Node { int value; Node*next; }; 2&#xff0c;分配节点 //之所以要分配节点原因是需要在分配函数中…

CodeForces - 372CWatching Fireworks is Fun+DP+单调队列优化

【题目描述】 CodeForces - 372CWatching Fireworks is Fun 题目的大概意思就是在一个编号为1…n的街道上现在按照时间顺序放烟花&#xff0c;每个烟花获得的幸福感为b−abs(a−x)b-abs(a-x)b−abs(a−x)&#xff0c;x为观看烟花的位置&#xff0c;为了提升我们的幸福感&#x…

双向链表的基本操作

1.双向链表的数据结构 typedef char DLinkType;typedef struct DLinkNode { DLinkType data; struct DLinkNode* next; struct DLinkNode* prev; }DLinkNode; 双向带头结点的链表有三个成员&#xff0c; 一个是数据&#xff0c; 一个是指针 next 指向当前结点的下一个结点&…

匿名管道

1.进程通信的目的 (1) 数据传输: 一个进程需要将它的数据传输给另一个进程     (2) 资源共享: 多个进程之间共享同样的资源     (3) 通知事件: 一个进程需要向另一个或一组进程发送消息, 通知它们发生了什么事情 2.管道 管道是一种进程之间通信的一种方式, 我们把从…

Currency Exchange——最短路Bellman-Ford算法

【题目描述】 Several currency exchange points are working in our city. Let us suppose that each point specializes in two particular currencies and performs exchange operations only with these currencies. There can be several points specializing in the sam…

C++实现String类

http://blog.csdn.net/randyjiawenjie/article/details/6709539 C实现String类&#xff0c;还没有完成&#xff0c;待继续。 有以下注意的点&#xff1a; &#xff08;1&#xff09;赋值操作符返回的是一个MyString&&#xff0c;而重载的返回的是一个MyString。其中的原因…

POJ 3370 Halloween treats——鸽巢原理+思维

【题目描述】 POJ 3370 Halloween treats Description Every year there is the same problem at Halloween: Each neighbour is only willing to give a certain total number of sweets on that day, no matter how many children call on him, so it may happen that a chi…

将信号量代码生成静态库以及动态库

1.信号量相关代码生成静态库 2.信号量相关代码生成动态库