Codeforces 1163E Magical Permutation [linear-yl, configured]

codeforces

Thinking

I followed the label point graph theory go, no one thought ......

Each number can be found in the arrangement of the collection is the number of XOR out.

Consider the answers upper bound is. If the can is less than \ (2 ^ k \) is constructed number \ (\ [0,2 ^ k- 1]) all the numbers, then the answer to this \ (K \) takes \ (\ max \) . It is clear that this must be an upper bound.

Consider can construct a set of solutions. The \ ([k-1 ^ 1,2 ] \) of the number of inserted carry out to obtain a set of linear Kiri greatly linearly independent set, then obviously it \ (size \) is \ (K \) . Because of its linearly independent, it is arbitrarily selected a subset of the exclusive OR obtained will not be the same and, therefore consider \ (0 \) on the left, then each time a different element or linearly independent of the group, to take over all set.

Recursion set may take: For size \ (m \) set of first \ (m-1 \) to take over, and then take the first \ (m \) elements, and then \ (m-1 \ ) collection take it again, we can only guarantee an adjacent set of a different position, and the set of all pairwise disjoint.

Code

#include<bits/stdc++.h>
clock_t t=clock();
namespace my_std{
    using namespace std;
    #define pii pair<int,int>
    #define fir first
    #define sec second
    #define MP make_pair
    #define rep(i,x,y) for (int i=(x);i<=(y);i++)
    #define drep(i,x,y) for (int i=(x);i>=(y);i--)
    #define go(x) for (int i=head[x];i;i=edge[i].nxt)
    #define templ template<typename T>
    #define sz 303030
    typedef long long ll;
    typedef double db;
    mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
    templ inline T rnd(T l,T r) {return uniform_int_distribution<T>(l,r)(rng);}
    templ inline bool chkmax(T &x,T y){return x<y?x=y,1:0;}
    templ inline bool chkmin(T &x,T y){return x>y?x=y,1:0;}
    templ inline void read(T& t)
    {
        t=0;char f=0,ch=getchar();double d=0.1;
        while(ch>'9'||ch<'0') f|=(ch=='-'),ch=getchar();
        while(ch<='9'&&ch>='0') t=t*10+ch-48,ch=getchar();
        if(ch=='.'){ch=getchar();while(ch<='9'&&ch>='0') t+=d*(ch^48),d*=0.1,ch=getchar();}
        t=(f?-t:t);
    }
    template<typename T,typename... Args>inline void read(T& t,Args&... args){read(t); read(args...);}
    char __sr[1<<21],__z[20];int __C=-1,__zz=0;
    inline void Ot(){fwrite(__sr,1,__C+1,stdout),__C=-1;}
    inline void print(register int x)
    {
        if(__C>1<<20)Ot();if(x<0)__sr[++__C]='-',x=-x;
        while(__z[++__zz]=x%10+48,x/=10);
        while(__sr[++__C]=__z[__zz],--__zz);__sr[++__C]='\n';
    }
    void file()
    {
        #ifdef NTFOrz
        freopen("a.in","r",stdin);
        #endif
    }
    inline void chktime()
    {
        #ifdef NTFOrz
        cout<<(clock()-t)/1000.0<<'\n';
        #endif
    }
    #ifdef mod
    ll ksm(ll x,int y){ll ret=1;for (;y;y>>=1,x=x*x%mod) if (y&1) ret=ret*x%mod;return ret;}
    ll inv(ll x){return ksm(x,mod-2);}
    #else
    ll ksm(ll x,int y){ll ret=1;for (;y;y>>=1,x=x*x) if (y&1) ret=ret*x;return ret;}
    #endif
//  inline ll mul(ll a,ll b){ll d=(ll)(a*(double)b/mod+0.5);ll ret=a*b-d*mod;if (ret<0) ret+=mod;return ret;}
}
using namespace my_std;

int n,K=20,m;
int a[sz],b[sz],lg2[sz];

int lb[sz],cnt;
bool insert(int x)
{
    drep(i,K,0) if (x>>i&1)
    {
        if (!lb[i]) return lb[i]=x,++cnt,1;
        x^=lb[i];
    }
    return 0;
}
int cur;
void dfs(int m)
{
    if (m==0) printf("%d ",cur);
    else dfs(m-1),cur^=b[m],dfs(m-1);
}

int main()
{
    file();
    read(n);
    rep(i,1,n) read(a[i]);
    sort(a+1,a+n+1);
    int ans=0;
    rep(i,2,sz-1) lg2[i]=lg2[i>>1]+1;
    for (int k=0,i=1;k<=K;k++)
    {
        while (i<=n&&lg2[a[i]]==k) insert(a[i]),++i;
        if (cnt==k+1) ans=k+1;
    }
    printf("%d\n",ans);
    rep(i,0,K) lb[i]=0;
    rep(i,1,n) if (lg2[a[i]]<ans&&insert(a[i])) b[++m]=a[i];
    dfs(m);
    return 0;
}

Guess you like

Origin www.cnblogs.com/p-b-p-b/p/11741238.html