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

题目大意:给你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 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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值