SGU275. To xor or not to xor(高斯消元求N个数中任取M个数求得异或最大值)

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 


题意就是给你N个数,然后从中任取M个数使得这M个数异或的值最大,最后输出最大值。

题解:把这N个数分解成二进制表示01表示。要使得最大,就得从最高位开始取值。然后记录每次取得的数。用高斯消元更新后面没有取的数就可以了。

#include<limits>
#include<queue>
#include<vector>
#include<list>
#include<map>
#include<set>
#include<deque>
#include<stack>
#include<bitset>
#include<algorithm>
#include<functional>
#include<numeric>
#include<utility>
#include<sstream>
#include<iostream>
#include<iomanip>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<cstring>
#include<ctime>

#define LL __int64
#define eps 1e-8
#define pi acos(-1)
#define INF 0x7fffffff
#define delta 0.98 //模拟退火递增变量
using namespace std;
int var,n;
int a[110][110];
int vis[110];
LL gg;
void Gauss(){
	int t=0;
	int r,col;
	LL ans=0; 
	for (col=var-1;col>=0;col--){
		int maxr=-1;
		for (int i=0;i<n;i++)
			if (a[i][col]==1 && !vis[i]){
				maxr=i;
				break;
			}
		if (maxr==-1 && a[n][col]==0) 
			ans+=gg;
		else if (maxr!=-1){
			vis[maxr]=1;
			ans+=gg;
			for (int i=0;i<=n;i++)
				if (a[i][col] && i!=maxr){
					for (int j=0;j<col;j++)
						a[i][j]^=a[maxr][j];
				}
		}
		gg>>=1;
	}
	printf("%lld\n",ans);
}
int main(){
	scanf("%d",&n);
	memset(a,0,sizeof(a));
	int i,j;
	gg=1;var=0;
	LL k;
	for (i=0;i<n;i++){
		scanf("%lld",&k);
		j=0;
		while (k){
			if (k&1) a[i][j]=1;
			k>>=1;
			j++;
		}
		var=max(var,j);
	}
	for (i=0;i<var-1;i++){
		gg<<=1;
		a[n][i]=1;
	}
	memset(vis,0,sizeof(vis));
	a[n][var-1]=1;
	Gauss();
	return 0;
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值