【BZOJ-4269】再见Xor 高斯消元 + 线性基

4269: 再见Xor

Time Limit: 10 Sec  Memory Limit: 512 MB
Submit: 131  Solved: 81
[Submit][Status][Discuss]

Description

给定N个数,你可以在这些数中任意选一些数出来,每个数可以选任意多次,试求出你能选出的数的异或和的最大值和严格次大值。

Input

第一行一个正整数N。
接下来一行N个非负整数。

Output

一行,包含两个数,最大值和次大值。

Sample Input

3
3 5 6

Sample Output

6 5

HINT

100% : N <= 100000, 保证N个数不全是0,而且在int范围内

Source

Solution

学线性基,还没完全学透,这是到纯模板题...于是...

Code

#include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<cstdio>
using namespace std;
#define maxn 100010
int a[maxn],n;
void Gauss()
{
    int tmp=0,i;
    for (int j=1<<30; j; j>>=1)
        {
            for (i=tmp+1; i<=n; i++)
                if (a[i]&j) break;
            if (i>n) continue;
            swap(a[++tmp],a[i]);
            for (i=1; i<=n; i++)
                if (i!=tmp && a[i]&j)
                    a[i]^=a[tmp];
        }
    n=tmp;
}
int main()
{
    scanf("%d",&n);
    for (int i=1; i<=n; i++) scanf("%d",&a[i]);
    Gauss();
    int maxx=0;
    for (int i=1; i<=n; i++) maxx^=a[i];
    int cmaxx=maxx^a[n];
    printf("%d %d
",maxx,cmaxx);
    return 0;
}
原文地址:https://www.cnblogs.com/DaD3zZ-Beyonder/p/5516424.html