题目:
http://acm.hdu.edu.cn/showproblem.php?pid=1517
题意:
Stan和Ollie玩游戏,初始给定一个数字p=1,两人轮流操作,每次可以用数字p乘以[2,9]内的任意一个数,再给出一个n,谁先使p>=n就获胜。问每次游戏的结果
思路:
可以直接找规律的,与2和9有关,写一写很快就发现了。也可以sg打表,因为范围比较大,用map而非数组,这里sg只有false和true两种值
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1000 + 10, INF = 0x3f3f3f3f;
int main()
{
int n;
while(~ scanf("%d", &n))
{
ll p = 1;
for(int i = 1; true; i++)
{
if(i & 1) p *= 9;
else p *= 2;
if(n <= p)
{
puts((i & 1) ? "Stan wins." : "Ollie wins.");
break;
}
}
}
return 0;
}
sg打表
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1000 + 10, INF = 0x3f3f3f3f;
map<ll, bool> sg;
map<ll, bool> vis;
bool dfs(ll x, ll n)
{
if(x >= n) return false;
if(vis.find(x) != vis.end()) return sg[x];
vis[x] = true;
for(int i = 2; i <= 9; i++)
{
if(! dfs(x * i, n)) return sg[x] = true;
}
return sg[x] = false;
}
int main()
{
ll n;
while(~ scanf("%lld", &n))
{
sg.clear(); vis.clear();
dfs(1, n);
puts(sg[1] ? "Stan wins." : "Ollie wins.");
}
return 0;
}
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1000 + 10, INF = 0x3f3f3f3f;
map<ll, int> sg;
map<ll, bool> vis;
int dfs(ll x, ll n)
{
if(x >= n) return 0;
if(vis.find(x) != vis.end()) return sg[x];
vis[x] = true;
bool use[10];
memset(use, 0, sizeof use);
for(int i = 2; i <= 9; i++)
{
use[dfs(x*i,n)] = true;
}
for(int i = 0; i < 10; i++)
if(! use[i])
{
return sg[x] = i;
}
}
int main()
{
ll n;
while(~ scanf("%lld", &n))
{
sg.clear(); vis.clear();
dfs(1, n);
puts(sg[1] ? "Stan wins." : "Ollie wins.");
}
return 0;
}