洛谷 [P3812] 线性基

异或空间下的线性基模版

异或空间下求线性基,本质还是高斯消元,参见
http://www.cnblogs.com/Mr-WolframsMgcBox/p/8562924.html
求最大值是一个贪心的过程,从高位到低位,如果异或起来可以更大,就异或

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#define ll long long
using namespace std;
ll n, num[100], lb[100];
int main() {
	cin >> n;
	for(int i = 1; i <= n; i++) {
		cin >> num[i];
		for(int j = 52; j >= 0; j--) {
			if(num[i] & (1ll << j)) {
				if(!lb[j]) {lb[j] = num[i]; break;}
				else num[i] ^= lb[j];
			}
		}
	}
	ll ans = 0;
	for(int i = 52; i >= 0; i--) {
		if((ans ^ lb[i]) > ans) ans ^= lb[i];
	}
	cout<<ans<<endl;
	return 0;
}
posted @ 2018-03-14 15:21  Mr_Wolfram  阅读(208)  评论(1编辑  收藏  举报