HDU 1729(石子)

本文介绍了一种基于SG函数的博弈游戏实现方法,通过计算最大必败点来确定游戏胜负。具体包括了如何根据箱子容量和当前石子数量来决定放置石子的数量,以及如何使用递归方式求解SG值。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目链接

题意

一共有n个箱子,每个箱子容量为S,当前的石子为C。每次想一个箱子放石子,放的数量不能超过当前石子数的平方,谁先放完谁赢。

AC

SG函数

  • 当C == S 时,sg(S, C) = 0
  • 当C > T 时,假设T为当前最大的必败点,则T满足 T + T * T < S, (T + 1) + (T + 1) * (T + 1) >= S
    sg(S, C) = mex(sg(S, C + 1), sg(S, C + 2) …… sg(S, S - 1), sg(S, S)) = S - C
    证明:sg(S, S) = 0, sg(S, S - 1) = mex(sg(S, S)) = 1 ………
  • 当C <= T,此时sg(S, C) = sg(T, C),递归求sg
#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <vector>
#include <map>
#include <string.h>
#include <cmath>
#include <algorithm>
#define N 100005
#define P pair<int,int>
#define mk(a, b) make_pair(a, b)
#define mem(a, b) memset(a, b, sizeof(a))
int inf = 0x3f3f3f3f;
#define ll long long
using namespace std;
int get_sg(int s, int c) {
    // 求最大必败点
    int q = sqrt(s);
    while (q + q * q >= s)
        q--;
    if (c > q)  return s - c;
    else    return get_sg(q, c);
}
int main(){
    int Case = 1;
    int n;
    while (cin >> n, n) {
        int ans = 0;
        while (n--) {
            int s, c;
            cin >> s >> c;
            ans ^= get_sg(s, c);
        }

        cout << "Case " << Case++ << ":" << endl;
        if (ans)    cout << "Yes\n";
        else    cout << "No\n";
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值