题解:P3179 [HAOI2015] 数组游戏

· · 题解

本题主要考察 SG 函数的运用。

什么是 SG 函数?

SG 函数,全程为 Sprague-Grundy,顾名思义,是由某位字母哥发现的,其本质就是把公平博弈状态映射为数值化的 SG 值,从而分析胜负策略‌,下面我们需要了解一些基本定义:

mex 运算

这个运算其实不只是在 SG 函数中才用到,还是挺常见的。

对于集合 S,\text{mex}(S) 表示不属于 S 中的最小非负整数,例如 \text{mex}({0,2,3})=1 等等。

SG 的递归状态

对于一个状态 x,其 SG 值等于所有后继状态 SG 值的 mex 运算结果,不通俗的表示为:

当 y 是 x 的后继状态,那么:

SG(x)=\text{mex}\{SG(y)\}

SG 定理

复合游戏的 SG 值为各子游戏 SG 值的异或和。若异或和为零,则为必败态,否则为必胜态,即:

SG(G)=SG(G_1)\oplus SG(G_2) \oplus \dots SG(G_n)

若结果为零则为必败态,否则为必胜态。

证明

根据 SG 函数的性质,只需要证明:

SG(A+B)=SG(A)\oplus SG(B)

即可。

设局面 A 的一个后继为 A',局面 B 的一个后继为 B',SG(A)=a,SG(B)=b,SG(A')=a',SG(B')=b'。

根据mex的性质可知:

这里我们发现,若先手选择了一个局面的后继使后继局面的 SG 函数值大于当前局面本身的 SG 函数值,后手显然可以让局面的 SG 函数值再次等于当前的局面的 SG 函数值。所以让后继大于当前局面的 SG 函数值是没有意义的。

所以我们接下来对局面后继的选择只会是 SG 函数值小于原局面的 SG 函数值的后继。

这里采用分类讨论,即归纳法:

1

当 a=b=0 时,显然成立。

2

假设 (A'+B) 和 (B'+A) 都满足定理。

所以得出:

$SG(A+B')$ 取遍 $[0,a\oplus b')$。 不妨再设 $c=a\oplus b=\sum_{i=1}^t2^{P_i}$,且满足 $P_i>P_{i+1}$。 因为 $a'>a$,所以 $a'\oplus b\neq c$。 因为 $b'>b$,所以 $b'\oplus a\neq c$。 我们找到 $c$ 在二进制下的最高位 $P_i$,这时一定有一个数的这一位为 $1$,不妨为 $a$,另一个数的这一位为 $0$,不妨设 $b$。 所以,$a'\oplus b$ 取值集合的并集一定包含 $[0,2^{P_i})$,将 $a'$ 的第 $P_i$ 位强制设为 $1$,只看所有的二进制位数低于 $P_i$ 的二进制段,再用同样的方法计算取值集合并集。 这时我们可以发现有这些集合: $$[0,2^{P_1}),[0,2^{P_2}),[0,2^{P_3}),\dots ,[0,2^{P_t})$$ 但是这些集合都要加上高位上强制设为 $1$ 的位置,所以实际应该为: $$[0,2^{P_1}),[2^{P_1},2^{P_1}+2^{P_2}),\dots,[\sum^{t-1}_{i=1}2^{P_i},\sum_{i=1}^{t}2^{P_i})$$ 它们的并集为: $$[0,\sum_{i=1}^t2^{P_i})$$ 又因为: $$\sum_{i=1}^t2^{P_i}=c$$ 所以 $a'\oplus b$ 与 $a\oplus b'$ 的取值并集取遍: $$[0,c)$$ 因为 $a'\oplus b\neq c,a\oplus b'\neq c$,所以: $$c=\text{mex}_{a'<a,b'<b}\{a'\oplus b,a\oplus b'\}$$ 所以: $$SG(A+B)=c=a\oplus b=SG(A)+SG(B)$$ 证毕。 ## 本题做法 首先如果考场上真出现了这题,那必须先打一遍暴力骗分。 ### 30分 状压和记搜 ### 50分 用刚刚证明的性质模拟即可,注意 $SG(x)>x$ 和 $SG(x)>0$ 的情况,时间复杂度 $O(n\log n)$。 ```cpp #include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #include<cmath> #include<algorithm> #include<vector> #define re register #define il inline #define ll long long #define max(a,b) ((a)>(b)?(a):(b)) #define min(a,b) ((a)<(b)?(a):(b)) #define fp(i,a,b) for(re int i=a;i<=b;i++) #define fq(i,a,b) for(re int i=a;i>=b;i--) using namespace std; const int mod=1e9+7,N=1e7+100; int n,q,w,SG[N],ans,viss[N]; il void getSG(re int x) { re int t=1,tot=0; viss[0]=x; while(1+1==2) { ++t; if(x*t>n) break; viss[SG[x*t]^tot]=x; tot^=SG[x*t]; } re int tmp=0;while(viss[tmp]==x) ++tmp; SG[x]=tmp; } int main() { n=gi();q=gi(); fq(i,n,1) getSG(i); while(q--) { ans=0; w=gi(); fp(i,1,w) ans^=SG[gi()]; puts(ans?"Yes":"No"); } return 0; } ``` ### 100分做法 打表万岁!! 打完函数值的表就可以发现只要 $\frac{n}{j}=\frac{n}{i}$,$SG(i)=SG(j)$。 把相同的值合成为一块即可。 ```cpp #include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #include<cmath> #include<algorithm> #include<vector> #define re register #define il inline #define ll long long #define max(a,b) ((a)>(b)?(a):(b)) #define min(a,b) ((a)<(b)?(a):(b)) #define fp(i,a,b) for(re int i=a;i<=b;i++) #define fq(i,a,b) for(re int i=a;i>=b;i--) using namespace std; const int mod=1e9+7,N=1e6+100; int n,q,w,SG[2][N],ans,blk,b[N],id,vis[N]; il ll gi() { re ll x=0,t=1; re char ch=getchar(); while(ch!='-'&&(ch<'0'||ch>'9')) ch=getchar(); if(ch=='-') t=-1,ch=getchar(); while(ch>='0'&&ch<='9') x=x*10+ch-48,ch=getchar(); return x*t; } il int getSG(re int x) { x=n/(n/x); if(x<=blk) return SG[0][x];else return SG[1][n/x]; } il void Pre() { for(re int i=1;i<=n;i=n/(n/i)+1) b[++b[0]]=n/(n/i); fq(i,b[0],1) { re int x=b[i],now=0;++id;vis[0]=id; for(re int j=x+x;j<=n;) { re int t=(n/(n/j))/x*x,tmp=(t-j)/x+1; vis[now^getSG(j)]=id; if(tmp&1) now^=getSG(j); j=t+x; } re int tmp=0;while(vis[tmp]==id) ++tmp; if(x<=blk) SG[0][x]=tmp;else SG[1][n/x]=tmp; } } int main() { n=gi();q=gi();blk=sqrt(n)+1; Pre(); while(q--) { ans=0;w=gi(); fp(i,1,w) ans^=getSG(gi()); puts(ans?"Yes":"No"); } return 0; } ```