Recommended blog: https://www.cnblogs.com/vb4896/p/6149022.html
https://blog.csdn.net/qaq__qaq/article/details/53812883
I don't think this is very easy to understand. It took a day and a half to understand a little bit and write a blog.
First of all, we need to clarify what kind of problems this linear basis can solve, such as dynamically adding elements, solving set XOR maximum
There is an increase operation here, add an element to the inside, sweep down from the highest bit, when the sweep reaches 1, and the position in the linear base is 0, add this element to the linear base, otherwise use this element to XOR Numbers in Linear Basis
int val[maxn][40];
void insert(int b[], int x){ for(int i = 30; i >= 0; i--){ if (x>>i&1){ if (!b[i]) {b[i] = x; break;} else x ^= b[i]; } } }
merge
void unit(int x,int y){ // brute force x into y for(int i=30;i>=0;--i) if(val[x][i]) insert (val[y],val[x][i]); }
Inquire
Query whether an element is in the collectionx
maximum value
Sweep from high to low
int cal(int p){ int years = 0; for(int i = 30; i >= 0; i--){ if ((ans^val[i]) > ans) { ans ^ = val [i]; } } return ans; }
minimum
int query(){ for(int i = 0; i <= 30; i++){ if (val[i]) return val[i]; } return 0; }