[LibreOJ] #6060 [2017 Shandong Round 1 Training Camp Day 1 / SDWC2018 Day1] Set - Solution
Main Idea
A godlike problem.
Notice that \(x_1 \oplus x_2 = xorsum\), so we try to give as many bits of \(xorsum\) as possible to \(x_2\). If some bit of \(xorsum\) is \(1\), just give it straight to \(x_2\); if it is \(0\), there are two cases, \(0, 0\) and \(1, 1\). So when we build the linear basis, prioritizing the higher bits that are \(0\) makes the answer larger (think about the process of finding the maximum XOR sum with a linear basis: positions with greater weight are inserted first).
Code
// LOJ6060.cpp
#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 1e5 + 200;
typedef long long ll;
int n, tot;
ll ai[MAX_N], tb[MAX_N], bi[MAX_N];
void insert(ll x)
{
for (int i = 1; i <= tot; i++)
if (x & (1LL << tb[i]))
{
if (bi[i] == 0)
{
bi[i] = x;
break;
}
x ^= bi[i];
}
}
int main()
{
scanf("%d", &n);
ll xorsum = 0;
for (int i = 1; i <= n; i++)
scanf("%lld", &ai[i]), xorsum ^= ai[i];
for (int i = 62; i >= 0; i--)
if (!(xorsum & (1LL << i)))
tb[++tot] = i;
for (int i = 62; i >= 0; i--)
if (xorsum & (1LL << i))
tb[++tot] = i;
for (int i = 1; i <= n; i++)
insert(ai[i]);
ll x2 = 0;
for (int i = 1; i <= tot; i++)
if (!(x2 & (1LL << tb[i])))
x2 ^= bi[i];
printf("%lld\n", xorsum ^ x2);
return 0;
}