博客园 首页 私信博主 显示目录 隐藏目录 管理 动画

hihoCoder.1509.异或排序(位运算 思路)

题目链接

\(Description\)

给定长为\(n\)的序列\(A\)。求有多少\(S\),满足\(0\leq S<2^{60}\),且对于所有\(i\in[1,n-1]\)\(a[i]^{\wedge}S\leq a[i+1]^{\wedge}S\)
\(n\leq20\)

\(Solution\)

考虑对于\(a,b\),有哪些\(S\)满足\(a^{\wedge}S\leq b^{\wedge}S\)
找出\(a^{\wedge}b\)的最高位\(1\)(也就是\(a,b\)不同的最高位),如果这一位\(a\)\(0\)\(b\)\(1\),那么\(S\)这一位必须是\(0\);否则\(S\)这一位必须是\(1\)
所以每个\(a[i]^{\wedge}S\leq a[i+1]^{\wedge}S\)实际上是限制了\(S\)的某一位。把所有限制算出来就行了。

//0ms	0MB
#include <cstdio>
#include <cctype>
#include <cstring>
#include <algorithm>
#define gc() getchar()
#define BIT 59
typedef long long LL;
const int N=66;

int ban[N];

inline LL read()
{
	LL now=0;register char c=gc();
	for(;!isdigit(c);c=gc());
	for(;isdigit(c);now=now*10+c-'0',c=gc());
	return now;
}

int main()
{
	memset(ban,0xff,sizeof ban);
	int n=read(); LL las=read();
	for(int i=2; i<=n; ++i)
	{
		LL now=read(),s=now^las;
		for(int j=BIT; ~j; --j)
		{
			if(!(s>>j&1)) continue;
			if(las>>j&1)
				if(!ban[j]) return puts("0"),0;
				else ban[j]=1;
			else
				if(ban[j]==1) return puts("0"),0;
				else ban[j]=0;
			break;
		}
		las=now;
	}
	LL ans=1;
	for(int i=BIT; ~i; --i) if(ban[i]==-1) ans<<=1ll;
	printf("%lld\n",ans);

	return 0;
}
posted @ 2018-10-13 22:11  SovietPower  阅读(194)  评论(0编辑  收藏  举报