LG3812 "template" linear baseline substrate

Problem Description

LG3812


answer

Linear is a base class at solving XOR problem data structure (data structure is not too ... it is a kind of metaphysical stuff)

For the number of columns \ (A \) , its linear group \ (D \) to appear \ (1 \) most significant bits of \ (I \) digits (here borrowed problem solution "handsome to alarms" ).

Construction method

Insert number for each attempt \ (the X-\) , find out where it is currently \ (1 \) is the highest level \ (POS \) .

If this time \ (d_pos \) already has a number, then put \ (x \) exclusive or \ (d_pos \) keep trying.

Otherwise insert, inserted successfully to immediatelybreak

Looking for answers

Take greedy idea, as long as exclusive or \ (d_i \) does not make the answer less, or just different.


\(\mathrm{Code}\)

#include<bits/stdc++.h>
using namespace std;

template <typename Tp>
void read(Tp &x){
    x=0;char ch=1;int fh;
    while(ch!='-'&&(ch<'0'||ch>'9')) ch=getchar();
    if(ch=='-'){
        fh=-1;ch=getchar();
    }
    else fh=1;
    while(ch>='0'&&ch<='9'){
        x=(x<<1)+(x<<3)+ch-'0';
        ch=getchar();
    }
    x*=fh;
}

#define int long long

int d[63],n;
int a[53],ans;

void add(int x){
    for(int i=51;i>=0;i--){
        if(x&(1ll<<i)){
            if(!d[i]){
                d[i]=x;break;
            }
            x=x xor d[i];
        }
    }
}

void solve(){
    for(int i=50;i>=0;i--){
        if((ans xor d[i])>ans) ans=ans xor d[i];
    }
}

signed main(){
    read(n);
    for(int i=1;i<=n;i++){
        read(a[i]);add(a[i]);
    }
    solve();
    printf("%lld\n",ans);
    return 0;
}

Guess you like

Origin www.cnblogs.com/liubainian/p/11621144.html