题目大意:给你n个数字,让你从n个数中,选择一些数,使得异或最大。
如果想使异或的值更大一些,那么就保证高位尽量为1,这样先让最后一列的结果等于1,然后解方程。当到达第i行时,通过第0~i-1行对第i行经行求解,如果有解就要把这一位的结果加上。
这里用低位表示的高位。
275. To xor or not to xor
time limit per test: 0.25 sec.
memory limit per test: 65536 KB
memory limit per test: 65536 KB
input: standard
output: standard
output: standard
The sequence of non-negative integers A1, A2, ..., AN is given. You are to find some subsequence Ai
1, Ai
2, ..., Ai
k (1 <= i
1 < i
2 < ... < i
k <= N) such, that Ai
1 XOR Ai
2 XOR ... XOR Ai
k has a maximum value.
Input
The first line of the input file contains the integer number N (1 <= N <= 100). The second line contains the sequence A1, A2, ..., AN (0 <= Ai <= 10^18).
Output
Write to the output file a single integer number -- the maximum possible value of Ai
1 XOR Ai
2 XOR ... XOR Ai
k.
Sample test(s)
Input
3 11 9 5
Output
14
#include <algorithm>
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <iomanip>
#include <stdio.h>
#include <string>
#include <queue>
#include <cmath>
#include <stack>
#include <map>
#include <set>
#define eps 1e-10
///#define M 1000100
#define LL __int64
///#define LL long long
///#define INF 0x7ffffff
#define INF 0x3f3f3f3f
#define PI 3.1415926535898
#define zero(x) ((fabs(x)<eps)?0:x)
const int maxn = 210;
using namespace std;
int a[maxn][maxn];
int n;
void Gauss()
{
LL ans = 0;
int i, j;
for(int k = 0; k < 61; k++)
{
a[k][n] = 1;
for(i = 0; i < k; i++)
{
for(j = 0; j < n; j++)
if(a[i][j]) break;
if(j < n && a[k][j])
for(; j <= n; j++) a[k][j] ^= a[i][j];
}
for(i = 0; i < n; i++)
if(a[k][i]) break;
if(i < n || (i == n && !a[k][n]))
ans += (1LL<<(60-k));
}
cout<<ans<<endl;
}
int main()
{
while(cin >>n)
{
memset(a, 0, sizeof(a));
LL x;
for(int i = 0; i < n; i++)
{
cin >>x;
for(int j = 60; j >= 0; j--)
if((1LL<<j)&x)a[60-j][i] = 1;
}
Gauss();
}
return 0;
}