|
关于sg函数这篇blog讲得很详细http://blog.iyunv.com/logic_nut/article/details/4711489。
sg函数的价值在于把复杂的游戏拆分成简单的游戏,然后通过计算出这些简单游戏的sg值得到复杂游戏的sg值。
求sg值的基本方法:是根据状态转移,有些问题可以找到规律,不能找到规律的可以通过模拟转移过程来求解。
在此蟹蟹ABacker教我sg函数的求法
#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn = 55;
int sg[maxn], n, k;;
inline int mex(bool vis[])
{
int res = 0;
while(vis[res])res++;
return res;
}
int SG(int n)
{
if(~sg[n]) return sg[n];
if(n<k) return 0;
else {
bool vis[maxn] = {0};
int bound = (n-k)>>1;
for(int i = 0; i <= bound; i++){//枚举后续状态
vis[SG(i)^SG(n-k-i)] = 1;
}
return sg[n] = mex(vis);
}
}
int main()
{
int T;
scanf("%d",&T);
for(int cas = 1; cas <= T; cas++){
scanf("%d%d",&n,&k);
fill(sg,sg+n+1,-1);
int ans ;
if(k == 1) { ans = n&1; }
else ans = SG(n);
printf("Case %d: %s\n",cas,ans?"Winning":"Losing");
}
return 0;
}
|
|
|