zoukankan      html  css  js  c++  java
  • CF1168E Xor Permutations

    邓老师在WC2021上讲的随机做法

    考虑每次随机一个(p)中没有确定的位置(p_i),枚举每个(v)判断(v operatorname{XOR} a_i)是否已经在(q)中出现,若存在这样的(v)就令(p_i = v,q_i = v operatorname{XOR} a_i),否则就随机一个未在(p)中出现的(v)(p_i = v,q_i = v operatorname{XOR} a_i)并把原来(v operatorname{XOR} a_i)所在的位置重新设为未确定。

    #include<bits/stdc++.h>
    using namespace std;
    #define fp(i,l,r) for(register int (i)=(l);(i)<=(r);++(i))
    #define fd(i,l,r) for(register int (i)=(l);(i)>=(r);--(i))
    #define fe(i,u) for(register int (i)=front[(u)];(i);(i)=e[(i)].next)
    #define mem(a) memset((a),0,sizeof (a))
    #define O(x) cerr<<#x<<':'<<x<<endl
    inline int read(){
        int x=0,f=1;char ch=getchar();
        while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
        while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
        return x*f;
    }
    void wr(int x){
        if(x<0)putchar('-'),x=-x;
        if(x>=10)wr(x/10);
        putchar('0'+x%10);
    }
    mt19937 gen(20040502);
    const int MAXN=4100;
    int n,a[MAXN],A[MAXN],B[MAXN],pa[MAXN],pb[MAXN],tmp[MAXN],cnt,tmp2[MAXN],tot;
    main(){
    	n=1<<read();fp(i,1,n)a[i]=read();
    	memset(A,-1,sizeof A);memset(B,-1,sizeof B);
    	while(clock()/CLOCKS_PER_SEC<0.9){
    		mem(pa);mem(pb);cnt=tot=0;
    		fp(i,1,n)if(A[i]!=-1)pa[A[i]]=i,pb[B[i]]=i;else tmp[++cnt]=i;
    		if(!cnt){
    			puts("Shi");
    			fp(i,1,n)wr(A[i]),putchar(' ');puts("");
    			fp(i,1,n)wr(B[i]),putchar(' ');puts("");
    			return 0;
    		}
    		fp(i,0,n-1)if(!pa[i])tmp2[++tot]=i;
    		int p=tmp[gen()%cnt+1],v,t;
    		fp(i,1,tot){
    			int t=a[p]^tmp2[i];
    			if(!pb[t]){
    				A[p]=tmp2[i];B[p]=t;goto ss;
    			}
    		}
    		v=tmp2[gen()%tot+1];
    		t=a[p]^v;A[pb[t]]=B[pb[t]]=-1;B[p]=t;A[p]=v;
    		ss:;
    	}
    	puts("Fou");return 0;
    }
    
  • 相关阅读:
    原型模式——浅复制与深复制
    初识Java反射
    建造者模式
    利用事件委托弥补观察者模式不足
    利用Java提供的Observer接口和Observable类实现观察者模式
    观察者模式
    再说单例模式的线程安全问题
    组合模式
    MyBatis3入门
    [Swift]LeetCode1216. 验证回文字符串 III | Valid Palindrome III
  • 原文地址:https://www.cnblogs.com/WinterSpell/p/14405470.html
Copyright © 2011-2022 走看看