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

题意:

  从n个数中选若干个数,使它们的异或和最大。n<=100

 

 


Solution

           经典的异或高斯消元。

//O(60*n)
#include <iostream>
using namespace std;
int n;
long long a[109];

int main()
{
    ios::sync_with_stdio();
    cin >> n;
    long long ans = 0;
    for (int i = 1; i <= n; ++i) cin >> a[i];
    for (int i = 62; i >= 0; --i) {
        for (int j = 1; j <= n; ++j) {
            if (a[j] & 1LL << i ) {
                long long t = a[j];
                if (! (ans & 1LL << i ) ) ans ^= t;
                for (int k = j; k <= n; ++k) {
                    if (a[k] & 1LL << i )
                        a[k] ^= t;
                }
            }
        }
    }
    cout << ans << endl;
    return 0;
}
O(63*n)

 

posted @ 2015-05-17 19:10  keambar  阅读(224)  评论(0编辑  收藏  举报