luogu P3812 【模板】线性基
题目描述
给定n个整数(数字可能重复),求在这些数中选取任意个,使得他们的异或和最大。
输入格式
第一行一个数n,表示元素个数
接下来一行n个数
输出格式
仅一行,表示答案。
#include<cstdio>
using namespace std;
#define ll long long
ll a[51],p[101],ans;
inline ll read(){
ll x = 0, f = 1; char ch = getchar();
while(ch > '9' || ch < '0'){ if(ch == '-')f = -1; ch = getchar(); }
while(ch >= '0' && ch <= '9'){ x = x * 10 + ch - '0'; ch = getchar(); }
return x * f;
}
signed main(){
int n=read();
for(int i=1;i<=n;i++) {
a[i]=read();
for(int j=62;j>=0;j--){
if(!(a[i]>>j))continue;
if(!p[j]){ p[j]=a[i]; break; }
a[i]^=p[j];
}
}
for(int i=62;i>=0;i--)if((ans^p[i])>ans)ans=ans^p[i];
printf("%lld",ans);
}
不以物喜,不以己悲
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步