@TopCoder - 12197@ XorAndSum


@description@

Given number N, the operation may be arbitrarily selected every two numbers, which will be replaced by a different number or two.

After any required operations, and the final maximum value of the number of all.

Class:
XorAndSum
Method:
maxSum
Parameters:
long[]
Returns:
long

sample:
{1,2,3}
Returns: 8

constraints
the number of the number of N <= 50, to ensure that the number of all weights <= 10 ^ 15.

@solution@

Maximum, XOR these keywords is not difficult to think of linear groups.
At the same time it can be found, given the operation subject quite as Gaussian elimination, the exclusive-or operation to another equation equation.
This is more determined we write linear groups.

We can practice analogy Gaussian elimination, the number of outer group of all linear elimination to 0, and then the maximum can be obtained by XOR exclusive or into a linear group.
Consider the number of linear groups how to fetch a maximum value.

We will continue the analogy yl linear Gaussian elimination for simplicity, the main element of each row (if any) into the column of all of the exclusive-OR 0.
then? Can be found in a group of size k may be linear or an exclusive number k ^ 2 species, and we have a total of k principal components, a main element is determined for each selected program is not selected is the number 2 ^ k. And then the two are one to one.
We hope that the final number of each group is the maximum linear: selecting all of the main elements that arrangement. But we can see that the definition linear group can be simple if there is evidence to the contrary more than two principal components in each row where their columns are 1, will lead to conflicts.
This means that for main-element is located, there must be a 0, only one will be an exception. Obviously we will put this exception largest primary is optimal.
Thus the maximum linear group and can be constructed of: selecting a number of all primary elements (i.e., the maximum value can be the exclusive OR), a number of other PCA always missing.

I do not know what I wrote above. You can also look at yhn seniors blog .

@accepted code@

#include<cstdio>
#include<vector>
#include<algorithm>
using namespace std;
typedef long long ll;
class XorAndSum{
    public:
    ll b[60 + 5];
    void insert(ll x) {
        for(int i=60;i>=0;i--)
            if( x & (1LL<<i) ) {
                if( b[i] == 0 )
                    b[i] = x;
                x ^= b[i];
            }
    }
    ll get_max() {
        for(int i=60;i>=0;i--)
            for(int j=i-1;j>=0;j--)
                if( b[i] & (1LL<<j) )
                    b[i] ^= b[j];
        ll ret = 0;
        for(int i=60;i>=0;i--)
            ret ^= b[i];
        return ret;
    }
    ll maxSum(vector<ll>v) {
        for(int i=0;i<v.size();i++)
            insert(v[i]);
        ll x = get_max(), ans = v.size()*x;
        bool fir = true;
        for(int i=60;i>=0;i--)
            if( b[i] ) {
                if( fir )
                    fir = false;
                else
                    ans = ans - x + (x^b[i]);
            }
        return ans;
    }
};

@details@

Teacher: This entry is not linear base of the title? Your exam time can not afford to even write this?

I:……

How to do the whole world is mocking me. . .

Guess you like

Origin www.cnblogs.com/Tiw-Air-OAO/p/11128702.html