给定n个数,求取若干个数,使得这些数异或和最大.贪心:按位从高到低进行处理,若某一个数的第i位的值是1,则将其他第i位上是1的数都与之异或,可以证明这样贪心地做是正确的,顺带提一句,这样的过程很像gauss消元.
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cctype>
#include<algorithm>
using namespace std;
unsigned long long a[105];
int n;
unsigned long long FastScan() {
unsigned long long ret = 0;
char c;
while(!isdigit(c = getchar()));
ret = c - '0';
while(isdigit(c = getchar())) {
ret = ret * 10 + (c - '0');
}
return ret;
}
void Solve();
int main() {
scanf("%d", &n);
for(int i = 1; i <= n; i++) a[i] = FastScan();
Solve();
return 0;
}
void Solve()
{
for (int i = 63; i >= 0; -- i) {
for (int j = 1; j <= n; ++ j) {
if ((i == 63) || ((i < 63) && ((a[j] >> i) & 1) == 1 && a[j] < (1ull << (i + 1)))) {
for (int k = 1; k <= n; ++ k) {
if (j != k && (a[k] >> i) & 1) {
a[k] ^= a[j];
}
}
break;
}
}
}
unsigned long long res = 0;
for (int i = 1; i <= n; i ++) {
res ^= a[i];
}
printf("%llu\n", res);
}