显然整个局面的sg函数值为所有石头堆的sg函数值的异或和,那我们只需要计算每堆石头的sg函数值即可
我们考虑一堆石头数量为x,当它被拆分成m堆后,这m堆要么是
⌊
x
m
⌋
\lfloor\frac{x}{m}\rfloor
⌊mx⌋,要么是
⌊
x
m
⌋
+
1
\lfloor\frac{x}{m}\rfloor+1
⌊mx⌋+1,显然有不同的奇偶性,判断一下奇偶性就可以递归算出分解完后这一堆的sg函数值,最后统计一下所有
2
≤
i
≤
m
2\le i \le m
2≤i≤m的选法取mex即可得到这一堆的sg函数值
我们可以发现,总共的状态比较少,所以记忆化一下就可以降低大量复杂度
Code:
#include<bits/stdc++.h>
using namespace std;
inline int read(){
int res=0,f=1;char ch=getchar();
while(!isdigit(ch)) {if(ch=='-') f=-f;ch=getchar();}
while(isdigit(ch)) {res=(res<<1)+(res<<3)+(ch^48);ch=getchar();}
return res*f;
}
const int N=1e5+5;
int sg[N];
inline int calc(int x){
if(sg[x]!=-1) return sg[x];
bool pt[N];
memset(pt,false,sizeof(pt));
for(int i=2,j;i<=x;i=j+1){
j=min(x/(x/i),x);
int now=0,div=x/i,res=x%i;
if(j-i+1<2){
if(res&1) now^=calc(div+1);
if((i-res)&1) now^=calc(div);
pt[now]=true;
}
else{
if(div&1){
if((i-res)&1) now^=calc(div);
pt[now]=true;
now^=calc(div+1);
pt[now]=true;
}
else{
if(res&1) now^=calc(div+1);
pt[now]=true;
now^=calc(div);
pt[now]=true;
}
}
}
for(int i=0;;i++) if(pt[i]==false) {sg[x]=i;break;}
return sg[x];
}
int main(){
memset(sg,-1,sizeof(sg));
int t=read(),F=read();for(int i=0;i<F;i++) sg[i]=0;
while(t--){
int n=read(),ans=0;
for(int x,i=1;i<=n;i++) x=read(),ans^=calc(x);
if(ans==0) cout<<"0 ";
else cout<<"1 ";
}
return 0;
}