http://acm.sgu.ru/problem.php?contest=0&problem=275
题意:
给你N个正整数,要求你从中选择若干个数,然后进行异或,求一个最后的最大异或值。N<=100
思路:
异或是一种二进制的运算,所以我们先将给定的10进制数化为2进制,每个10进制数i,对应的二进
制的第j位记为:g[i][j] , 这样我们就可以得到以下的一系列方程:
g[0][0]*b[0] + g[1][0]*b[1] + g[2][0]*b[2] + ... + g[N-1][0]*b[N-1] = ans[0] ;
g[0][1]*b[0] + g[1][1]*b[1] + g[2][1]*b[2] + ... + g[N-1][1]*b[N-1] = ans[1] ;
#include<stdio.h>
#include<string.h>
#include<iostream>
using namespace std ;
typedef long long LL ;
int N ;
const int MAXN = 110 ;
LL a[MAXN] ;
int g[70][MAXN] ;
int NN ;
void gauss(){
int row , col ;
row = NN - 1 ;
LL ans = 0 ;
for( ;row>=0 ;row-- ){
col = 0 ;
ans <<= 1 ;
for( ; col<N ;col++){
if( g[row][col] ) break ;
}
if( col == N ){
if( g[row][N] == 0 ) ans |= 1 ;
continue ;
}
ans |= 1 ;
for(int r=row-1;r>=0;r--){
if( g[r][col] == 0 ) continue ;
for(int j=col ; j<=N ; j++){
g[r][j] = g[r][j] ^ g[row][j] ;
}
}
}
cout << ans << endl ;
//printf("%lld\n",ans) ;
}
int main(){
while(cin >> N){
for(int i=0;i<N;i++) cin >> a[i] ;
NN = -1 ;
memset(g, 0 , sizeof(g));
for(int i=0;i<N;i++){
LL nn = a[i] ;
int c = 0 ;
while( nn ){
g[c++][i] = (nn&1) ;
nn >>= 1 ;
}
if( NN < c ) NN = c ;
}
for(int i=0;i<NN;i++) g[i][N] = 1 ;
gauss() ;
}
return 0 ;
}
本文介绍了一种解决最大异或值问题的方法,通过对输入的十进制数转换为二进制,并采用类似高斯消元法的技术来找到能够实现最大异或结果的数的选择方案。
578

被折叠的 条评论
为什么被折叠?



