您的位置:首页 > 其它

HDU 1517 A Multiplication Game SG打表

2017-08-24 11:05 399 查看

题目:

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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: