首先求出线性基和异或和的最大值,那么线性基之外的元素都可以异或成最大值,线性基中最大值异或成最大值,剩下的值跟最大值异或。
#include<cstdio>
#include<algorithm>
using namespace std;
#define LL long long
LL a[55],f[55],ans,x;
int n,cnt,p;
int main()
{
scanf("%d",&n);
for (int i=1;i<=n;i++) scanf("%lld",&a[i]);
cnt=0;
for (int i=50,j=1;i>=0;i--)
{
p=-1;
for (int k=j;k<=n;k++)
if ((a[k]>>i)&1)
{
p=k;
break;
}
if (p==-1) continue;
swap(a[j],a[p]);
for (int k=1;k<=n;k++)
if (k!=j&&((a[k]>>i)&1)) a[k]^=a[j];
cnt=j++;
}
for (int i=1;i<=cnt;i++) x^=a[i];
for (int i=2;i<=cnt;i++) ans+=a[i]^x;
ans+=x*(n-cnt+1);
printf("%lld\n",ans);
}