今天學習了神奇的線性基,主要是在解決異或問題時比較有用。
詳細的解釋和證明有大佬珠玉在前,如果感興趣可以移步
補充一下自己的理解:
可以聯系線性代數極大無關組進行理解,線性基就相當于異或的向量空間中的極大無關組(線性代數中的是加減的向量空間,因此線性基有極大無關組的性質:
- 向量的個數一定
- 可以表示向量空間內任意一個向量
- 向量空間中不包括零向量
但是我們在計算線性基的時候為什么要從最高位開始選呢?這其實只是為了方便使用,相當于我們在求極大無關組的時候習慣于從左往右進行初等變換的習慣一樣。同樣的,我們也可以從低位開始選,也是不會影響的,可是這樣選出來不方便我們使用(從高位到低位方便求最大值最小值)
在求第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;
}