高性能网站建设指南 京东,浙江壹设软装设计有限公司,上海网站建设选缘魁-企查,源码哥网站的模板今天学习了神奇的线性基#xff0c;主要是在解决异或问题时比较有用。 详细的解释和证明有大佬珠玉在前#xff0c;如果感兴趣可以移步 补充一下自己的理解#xff1a; 可以联系线性代数极大无关组进行理解#xff0c;线性基就相当于异或的向量空间中的极大无关组#xff…今天学习了神奇的线性基主要是在解决异或问题时比较有用。 详细的解释和证明有大佬珠玉在前如果感兴趣可以移步 补充一下自己的理解 可以联系线性代数极大无关组进行理解线性基就相当于异或的向量空间中的极大无关组线性代数中的是加减的向量空间因此线性基有极大无关组的性质
向量的个数一定可以表示向量空间内任意一个向量向量空间中不包括零向量
但是我们在计算线性基的时候为什么要从最高位开始选呢这其实只是为了方便使用相当于我们在求极大无关组的时候习惯于从左往右进行初等变换的习惯一样。同样的我们也可以从低位开始选也是不会影响的可是这样选出来不方便我们使用从高位到低位方便求最大值最小值 在求第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 CA 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 2345. 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(T30), indicates there are T test cases. For each test case, the first line is an integer N(1N10000), 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(1Q10000), 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 5Sample Output
Case #1:
1
2
3
-1
Case #2:
0
1
2
3
-1【AC代码】
#includecstdio
#includecstring
#includeiostream
#includealgorithm
#includevector
#includequeue
#includeset
#includeclimits
#includecstdlib
#includecmathusing namespace std;typedef long long ll;const int MAXN10005;
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));cntflag0;}inline bool insert(ll x){for(int i62;i0;--i)if(x(1lli)){if(b[i])x^b[i];else{b[i]x;return true;}}flag1;return false;}ll get_max(){ll ret 0;for(int i62;i0;--i)if((ret^b[i])ret)ret^b[i];return ret;}ll get_min(){if(flag)return 0;for(int i0;i62;i)if(b[i])return b[i];return 0;}inline void rebuild(){for(int i 1;i 62;i)if(b[i])for(int j0;ji;j)if(b[i](1llj))b[i]^b[j];for(int i0;i62;i)if(b[i])p[cnt]b[i];}ll kth(ll k){if(flag)--k;if(k0)return 0;ll ret 0;if(k(1llcnt))return -1;for(int i0;icnt-1;i)if(k(1lli))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 Case1;CaseT;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;
}