HDU 1729 Stone Game

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1729


题意:一个组合博弈论的游戏,有n个盒子,每个盒子有一个容量上限,且每一个盒子里都有一定量的石子。现在两个人轮流向盒子里加石子,一次加石子的范围是1到该盒子原石子的平方,但是盒子里的石子不能超过容量。


思路:计算每堆的sg值,然后异或起来。我们可以发现一个小规律,当x*x+x >= 容量,也就是当前状态可以到达剩下的所有状态,那么sg[x] = 容量 - x。如果不能一次放满,而由于范围不超过10^6,所以实际需要计算的sg也就1000左右,一个个计算即可。


#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <utility>
using namespace std;

#define rep(i,j,k) for (int i=j;i<=k;i++)
#define Rrep(i,j,k) for (int i=j;i>=k;i--)

#define Clean(x,y) memset(x,y,sizeof(x))
#define LL long long
#define ULL unsigned long long
#define inf 0x7fffffff
#define mod 100000007
const int maxn = 1009;

int cal( int have , int up )
{
    int sg[maxn];
    bool f[maxn];
    int x = 1;
    while( 1 )
    {
        if ( x * x + x < up )
            x++;
        else break;
    }
    if ( have >= x ) return up-have;//一次可以放满
    Rrep(i,x-1,have)
    {
        Clean(f,false);
        int R = i*i;
        rep(j,1,R)
        //这里当 i+j >= x 时不需要考虑了
        //当 i每减少1,相比于(i+1)来说会丢失掉2i+1个值,而多了sg[i+1]一个值,所以不可能和后面衔接上,就无需记录了。
        if ( i+j < x )
            f[ sg[i+j] ] = true;
        int j = 0;
        while( f[j] ) j++;
        sg[i] = j;
    }
    return sg[have];
}
int n;
int main()
{
    int kase = 0;
    while( scanf("%d",&n) == 1 )
    {
        if ( !n ) break;
        printf("Case %d:\n",++kase);
        int tx,ty;
        int ans = 0;
        rep(i,1,n)
        {
            scanf("%d%d",&tx,&ty);
            if ( tx != 0 && ty != 0 )
                ans ^= cal( ty , tx );
        }
        puts( ans ? "Yes" : "No" );
    }
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值