input:
2 5 10 10 10 10 10 4 1 1 2 1
output:
10 3
题目大意:给我们一段长度为n的区间,我们可以任意选择一段区间将这段区间内所有的数字变成这段区间的异或,问我们如果最终将这段长度为n的序列变成相同的数字,这个数组最大是多少。
解题思路:线性基模板题。线性基主要就是 用来解决一段区间异或的最大值和最小值以及第k大值。线性基不懂的可以看一下这位大佬的博客,讲解的非常清楚:(187条消息) 线性基详解_Hypoc_的博客-CSDN博客_线性基
看了这篇博客后相信大家对线性基都有了大概的了解,所以直接上代码了:
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
using namespace std;
const int N=1e5+10;
int n;
long long d[110],a[N];
void add(long long x)
{
for(int i=60;i>=0;i--)
{
if(x&(1ll<<i))
{
if(d[i])
x^=d[i];
else
{
d[i]=x;
break;
}
}
}
}
int main()
{
std::ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int T;
cin>>T;
while(T--)
{
cin>>n;
memset(d,0,sizeof d);
for(int i=1;i<=n;i++)
{
cin>>a[i];
add(a[i]);
}
long long ans=0;
for(int i=60;i>=0;i--)
if((ans^d[i])>ans)
ans=ans^d[i];
cout<<ans<<endl;
}
return 0;
}