算法学习:线性基
线性基:
通过将序列中的数字转化为二进制,表现为矩阵形式,于是通过矩阵的相关知识
就能够快速的求取区间内的最大异或和
模板:洛谷【P3812】
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<cstdio> 2 #include<iostream> 3 #define ll long long 4 using namespace std; 5 const int MAXN = 62; 6 ll p[MAXN], a[MAXN]; 7 void insert(ll x) 8 { 9 for (int i = 50; i >=0; i--) 10 { 11 if (!(x&(1ll<<i))) 12 { 13 continue; 14 } 15 if (!p[i]) 16 { 17 p[i] = x; 18 break; 19 } 20 x ^= p[i]; 21 } 22 return; 23 } 24 int main() 25 { 26 ll n; 27 scanf("%lld", &n); 28 for (int i = 1; i <= n; i++) 29 { 30 scanf("%lld", &a[i]); 31 insert(a[i]); 32 } 33 ll ans = 0; 34 for (int i = 50; i>=0; i--) 35 { 36 if ((ans^p[i]) > ans) 37 ans ^= p[i]; 38 } 39 printf("%lld", ans); 40 return 0; 41 }