记忆化搜索处理Multi-SG函数。
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <map>
#include <stack>
#include <set>
using namespace std;
const int N=2e3+1;
int n;
int sg[N];
int dfs(int x)
{
if (~sg[x]) return sg[x];
if (x<=0) return 0;
bool vis[N];
memset(vis,false,sizeof(vis));
for (register int i=1; i<=x; ++i) vis[dfs(i-3)^dfs(x-i-2)]=true;
for (register int i=0; i<=2000; ++i) if (!vis[i]) {sg[x]=i; break;}
return sg[x];
}
int main(){
while (~scanf("%d",&n))
{
memset(sg,-1,sizeof(sg));
if (dfs(n)) puts("1");
else puts("2");
}
return 0;
}
博弈论做厌了,换个算法刷吧。
留个坑:Nimk问题。