[LOJ]#2126. 「HAOI2015」数组游戏 博弈 SG函数

Description

有一个长度为N的数组,甲乙两人在上面进行这样一个游戏:首先,数组上有一些格子是白的,有一些是黑的。然后两人轮流进行操作。每次操作选择一个白色的格子,假设它的下标为x。接着,选择一个大小在1~n/x之间的整数k,然后将下标为x、2x、…、kx的格子都进行颜色翻转。不能操作的人输。现在甲(先手)有一些询问。每次他会给你一个数组的初始状态,你要求出对于这种初始状态他是否有必胜策略。

Solution

许久没有做博弈题,又忘了这类题的一个性质……
就是说这种状态的SG值就是若干个子状态的SG值异或起来,也就是说每个白色格子是独立的,算出每个白色格子的SG值即可。
然后显然 ⌊ n i ⌋ \lfloor {n\over i} \rfloor in相同的SG值也相同,因为 S G i = m e x { S G 2 i , S G 2 i   x o r   S G 3 i . . . } SG_i=\text mex\{SG_{2i},SG_{2i}\ xor\ SG_{3i}...\} SGi=mex{SG2i,SG2i xor SG3i...},所以一共有 n \sqrt n n 种不同的SG值,暴力求每一种的时候也要分块。
复杂度只知道小于 O ( n ) O(n) O(n),具体不清楚。

Code

#include<bits/stdc++.h>
using namespace std;
#define LL long long
#define pa pair<int,int>
const int Maxn=70000;
const int mod=3007003;
const int inf=2147483647;
int read()
{
	int x=0,f=1;char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
	while(ch>='0'&&ch<='9')x=(x<<3)+(x<<1)+(ch^48),ch=getchar();
	return x*f;
}
int Hash[mod],V[mod];
void insert(int x,int v)
{
	int t=x%mod;
	while(Hash[t])t=((t==mod-1)?0:t+1);
	Hash[t]=x;V[t]=v;
}
int query(int x)
{
	int t=x%mod;
	while(Hash[t]&&Hash[t]!=x)t=((t==mod-1)?0:t+1);
	if(!Hash[t])return -1;
	return V[t];
}
int n,p[Maxn],sg[Maxn],lp=0;
int mark[Maxn],tim=0;
int main()
{
	n=read();
	int pos,cnt=0;
	for(int i=1;i<=n;i=pos+1)pos=n/(n/i),p[++lp]=i,insert(n/i,lp);
	sg[lp]=1;
	for(int i=lp-1;i;i--)
	{
		int x=p[i],m=n/x,t=0,last=1,c=1;++tim;
		for(int j=2;j<=m;j=pos+1)
		{
			pos=m/(m/j);
			mark[t]=mark[t^sg[query(m/j)]]=tim;
			if((pos-last)&1)t^=sg[query(m/j)];
			last=pos;
		}
		while(mark[c]==tim)c++;
		sg[i]=c;
	}
	int T=read();
	while(T--)
	{
		int w=read(),ans=0;
		while(w--)ans^=sg[query(n/read())];
		if(!ans)puts("No");
		else puts("Yes");
	}
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值