您的位置:首页 > 其它

SGU 275. To xor or not to xor(高斯消元)

2014-07-30 18:23 246 查看
题目大意:给你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

input: standard

output: standard

The sequence of non-negative integers A1, A2, ..., AN is given. You are to find some subsequence Ai1, Ai2, ..., Aik (1 <= i1 < i2 < ... < ik <= N) such, that Ai1 XOR Ai2 XOR
... XOR Aik 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 Ai1 XOR Ai2 XOR ... XOR Aik. 

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]
= 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]
))
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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: