首页 > 其他 > 详细

CF1168E Xor Permutations

时间:2021-02-15 23:08:30      阅读:50      评论:0      收藏:0      [点我收藏+]

邓老师在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;
}

CF1168E Xor Permutations

原文:https://www.cnblogs.com/WinterSpell/p/14405470.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!