275. To xor or not to xor(高斯消元求最大亦或值)

来源:互联网 发布:soundhound mac 编辑:程序博客网 时间:2024/06/08 13:39

题目地址:http://acm.sgu.ru/problem.php?contest=0&problem=275

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 Ai1, Ai2, ..., Aik (1 <= i1 < i2 < ... < ik <= N) such, that Ai1 XOR Ai2 XOR ... XOR Aik 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 Ai1 XOR Ai2 XOR ... XOR Aik

Sample test(s)

Input
3 11 9 5 
Output
14 

【解析】:

题意:从输入的n个数中,选出一部分,亦或起来,使得亦或值最大。

看了别人的代码解析,还是理解不了,模仿着写了个代码,虽然AC了。但是还是不理解到底是怎么做到的。

(等我懂了再补解析吧。望同志能给指点,评论区指点迷津)

【代码】:

#include <stdio.h>#include <string.h>#define mset(a,i)  memset(a,i,sizeof(a))using namespace std;typedef long long ll;ll a[64];bool vis[105];int main(){int n;scanf("%d",&n);for(int i=0;i<n;i++)scanf("%lld",&a[i]);a[n]=0x7fffffffffffffff;ll ans=0;mset(vis,0);for(int i=62;i>=0;i--)//从最高位开始选{int flag=1;for(int j=0;j<n;j++)//寻找该位是1的数,去亦或进其他数里{if(flag&&!vis[j]&&(a[j]&(1ll<<i)))//a[j]没有选&&该位为1{vis[j]=1;for(int k=0;k<=n;k++)if(k!=j&&(a[k]&(1ll<<i)))a[k]^=a[j];flag=0;}}if(flag==0||(flag&&(a[n]&(1ll<<i))==0))ans+=(1ll<<i);}printf("%lld\n",ans);return 0;}


阅读全文
0 0
原创粉丝点击