您现在的位置是:首页 > 正文

线性基入门

2024-04-01 02:03:12阅读 3

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

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

但是我们在计算线性基的时候为什么要从最高位开始选呢?这其实只是为了方便使用,相当于我们在求极大无关组的时候习惯于从左往右进行初等变换的习惯一样。同样的,我们也可以从低位开始选,也是不会影响的,可是这样选出来不方便我们使用(从高位到低位方便求最大值最小值)
在求第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;
}

网站文章