HDU-1847 Good Luck in CET-4 Everybody!
Good Luck in CET-4 Everybody!
一堆石子,只能取2的幂次方个,不能取的就输,问先手是否必胜
sg函数模板
这是一维的sg函数,\(sg[i] = 0\) 代表只剩下i个石子的时候就是先手必输态,其余的就是先手必胜态
sg函数有点类似于记忆化搜索
#include <iostream>
#include <vector>
using namespace std;
const int maxn = 1e3 + 10;
int sg[maxn], vis[maxn];
vector<int>f;
void get_sg(int n)
{
for(int i=1; i<=n; i++)
{
for(int j=0; j<=n; j++) vis[j] = 0;
for(int j=0; j<f.size() && f[j]<=i; j++)
vis[sg[i-f[j]]] = 1;
for(int j=0; j<=n; j++)
{
if(vis[j] == 0)
{
sg[i] = j;
break;
}
}
}
}
int main()
{
int maxx = 1000;
int now = 1;
while(now <= 1000)
{
f.push_back(now);
now <<= 1;
}
get_sg(maxx);
int n;
while(cin >> n)
{
if(sg[n] == 0) cout << "Cici" << endl;
else cout << "Kiki" << endl;
}
return 0;
}