取石子

取石子

https://ac.nowcoder.com/acm/problem/15972

设d[i][j]表示先手面临两堆石子分别为i,j时的取胜状态。
直接求SG函数即可。
实际上可以通过值看出来两堆石子和为奇数时候先手必胜,否则后手胜。
因为每次取的都为奇数并不会对总的石子数奇偶性造成改变。所以奇数时
即会有奇数轮,先手必胜。

#include <bits/stdc++.h>
#include <unordered_map>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;

#ifdef LOCAL
#define debug(x) cout << "[" __FUNCTION__ ": " #x " = " << (x) << "]\n"
#define TIME cout << "RuningTime: " << clock() << "ms\n", 0
#else
#define TIME 0
#endif
#define hash_ 1000000009
#define Continue(x) { x; continue; }
#define Break(x) { x; break; }
const int mod = 998244353;
const int N = 1e5 + 10;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3f;
#define gc p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1000000, stdin), p1 == p2) ? EOF : *p1++;
inline int read(){ static char buf[1000000], *p1 = buf, *p2 = buf; register int x = false; register char ch = gc; register bool sgn = false; while (ch != '-' && (ch < '0' || ch > '9')) ch = gc; if (ch == '-') sgn = true, ch = gc; while (ch >= '0'&& ch <= '9') x = (x << 1) + (x << 3) + (ch ^ 48), ch = gc; return sgn ? -x : x; }
ll fpow(ll a, int b, int mod) { ll res = 1; for (; b > 0; b >>= 1) { if (b & 1) res = res * a % mod; a = a * a % mod; } return res; }
int d[110][110];
void dfs(int x, int y)
{
    if (d[x][y] != -1)
        return ;
    if (x == 0 && y == 0)
        return void(d[x][y] = 1);
    int flag = 1;
    if (x > 9)
        dfs(x - 9, y), flag &= d[x - 9][y];
    if (x > 3)
        dfs(x - 3, y), flag &= d[x - 3][y];
    if (x > 1)
        dfs(x - 1, y), flag &= d[x - 1][y];
    if (y > 9)
        dfs(x, y - 9), flag &= d[x][y - 9];
    if (y > 3)
        dfs(x, y - 3), flag &= d[x][y - 3];
    if (y > 1)
        dfs(x, y - 1), flag &= d[x][y - 1];
    if (!flag)
        d[x][y] = 1;
    else
        d[x][y] = 0;
    return ;
}
int main()
{
#ifdef LOCAL
    freopen("E:/input.txt", "r", stdin);
#endif
    memset(d, -1, sizeof d);
    for (int i = 1; i <= 100; i++)
        for (int j = 1; j <= 100; j++)
        {
            dfs(i, j);
        }
    int n1, n2;
    while (cin >> n1 >> n2)
    {
        printf("%s\n", d[n1][n2] ? "win" : "lose");
    }
    return TIME;
}
全部评论

相关推荐

尊尼获获:闺蜜在哪?
点赞 评论 收藏
分享
3 收藏 评论
分享
牛客网
牛客企业服务