275. To xor or not to xor
time limit per test: 0.25 sec.
memory limit per test: 65536 KB
memory limit per test: 65536 KB
input: standard
output: 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;
}