题目链接:https://www.luogu.org/problem/P3812
题目大意:给定n个整数(数字可能重复),求在这些数中选取任意个,使得他们的异或和最大
题解:线性基的模板题,直接套模板!
AC代码:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<vector> 2 #include<cstdio> 3 #include<iostream> 4 #include<cmath> 5 #include<queue> 6 #include<stack> 7 #define numm ch-48 8 #define pd putchar(' ') 9 #define pn putchar(' ') 10 #define pb push_back 11 #define fi first 12 #define se second 13 #define fre1 freopen("1.txt","r",stdin) 14 #define fre2 freopen("2.txt","w",stdout) 15 using namespace std; 16 template <typename T> 17 void read(T &res) { 18 bool flag=false;char ch; 19 while(!isdigit(ch=getchar())) (ch=='-')&&(flag=true); 20 for(res=numm;isdigit(ch=getchar());res=(res<<1)+(res<<3)+numm); 21 flag&&(res=-res); 22 } 23 template <typename T> 24 void write(T x) { 25 if(x<0) putchar('-'),x=-x; 26 if(x>9) write(x/10); 27 putchar(x%10+'0'); 28 } 29 const int maxn=70; 30 const int N=60; 31 const int inf=0x3f3f3f3f; 32 const int INF=0x7fffffff; 33 typedef long long ll; 34 ll d[maxn],x; 35 int n; 36 void add(ll x) { 37 for(int i=50;~i;i--) 38 if(x&((1LL<<i))) { 39 if(!d[i]) { 40 d[i]=x; 41 break; 42 } 43 else x^=d[i]; 44 } 45 } 46 void work() { 47 for(int i=1;i<=50;i++) 48 for(int j=0;j<i;j++) 49 if(d[i]&(1LL<<j)) d[i]^=d[j]; 50 } 51 ll qmax() { 52 ll ans=0; 53 for(int i=50;~i;i--) 54 if((d[i]^ans)>ans) ans^=d[i]; 55 return ans; 56 } 57 int main() 58 { 59 read(n); 60 for(int i=1;i<=n;i++) 61 read(x),add(x); 62 work(); 63 write(qmax()); 64 return 0; 65 }