Spoj Xmax-xor Maximization

Source: Internet
Author: User


Xmax-xor maximization


Given a set of integers S = {A1, a2, A3, ... a| s| }, we define a function X on S as follows:
X (S) = A1 ^ A2 ^ a3 ^ ... ^ a| s|.
(^ stands for bitwise ' XOR ' or ' exclusive or ')



Given a set of N integers, compute the maximum of the x-function over all the subsets of the Given set.


Input


The first line of input contains a single integer n, 1 <= n <= 105.
Each of the next N lines contain an integer AI, 1 <= ai <= 1018.


Output


To the first line of output print the solution.


Example
Input:

3
1
2
4
Output:

7





Gaussian elimination is similar. Try to change into a triangular matrix. Each one, try to keep a 1.



such as matrices:



010000



001000



000100



000010



is an ideal matrix.


 
 

/* ***********************************************
Author        :guanjun
Created Time  :2016/9/8 15:30:01
File Name     :spoj_XMAX.cpp
************************************************ */
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <stdio.h>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <iomanip>
#include <list>
#include <deque>
#include <stack>
#define ull unsigned long long
#define ll long long
#define mod 90001
#define INF 0x3f3f3f3f
#define maxn 100010
#define cle(a) memset(a,0,sizeof(a))
const ull inf = 1LL << 61;
const double eps=1e-5;
using namespace std;
priority_queue<int,vector<int>,greater<int> >pq;
struct Node{
    int x,y;
};
struct cmp{
    bool operator()(Node a,Node b){
        if(a.x==b.x) return a.y> b.y;
        return a.x>b.x;
    }
};

bool cmp(ll a,ll b){
    return a>b;
}
ll a[maxn];
int main()
{
    #ifndef ONLINE_JUDGE
    freopen("in.txt","r",stdin);
    #endif
    //freopen("out.txt","w",stdout);
    int n;
    while(cin>>n){
        for(int i=1;i<=n;i++)cin>>a[i];
        sort(a+1,a+1+n,cmp);
        int row=1;
        for(int i=60;i>=0;i--){
            for(int j=row;j<=n;j++){
                if(a[j]&(1LL<<i)){
                    swap(a[row],a[j]);
                    for(int k=1;k<=n;k++){
                        if((a[k]&(1LL<<i))&&(k!=row)){
                            a[k]=a[k]^a[row];
                        }
                        //puts("YES");
                    }
                    row++;
                }
            }
        }
        ll ans=0LL;
        for(int i=1;i<=n;i++){
        //    cout<<a[i]<<endl;
            ans^=a[i];
        }
        cout<<ans<<endl;
    }
    return 0;
}





Spoj Xmax-xor Maximization


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.