HDOJ1729 [SG博弈]

题目:题目链接


这道题的意思是说:

现在有n个箱子,每个箱子有一个容积si和一个当前箱子内的石子数ci,两个人轮流往这n个箱子里面放石子,每次放的个数为1-ci*ci。例如箱子里有3颗石子,那么下一个人就可以放1~9颗石子最后不能放入石子的人输。


题目分析:
这是sg博弈,不过这个sg函数需要分析:

我们知道(s,s0肯定是必败态,此时已经没有可以放入的石子了。


首先我们假设c 是必败点,则有c+c*c<s(即使放了最大的c*c也不能放满) 
而c+1则是必胜点则有c+1+(c+1)*(c+1)>=s根据这两个公式我们可以求出小于s的最大必败点t。 

而t+1----s-1是必胜点因为(t+1---s-1都可以到达s这个点)


接下来分三种情况讨论:

当 c == t  时则此时一定是必败点直接返回0; 

当c > t 时 此时C是必胜点只要返回C的后继集合中的最小即可。。。而此时最小是s-c 
石子数:    s  s-1   s-2  s-3 .....  c .. .. t  
对应的sg值:0    1     2    3 .....  s-c ... 0 

而当 c < t 时此时将t当做s继续递归下去。。因为刚开是的s始终为必败点

这个sg值的得到还是比较麻烦的难过

上代码:

#include <iostream>
#include <cstdio>
#include <string>
#include <string.h>
#include <map>
#include <vector>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <cmath>
#include <queue>
#include <set>
#include <stack>
using namespace std;
int min(int a, int b)
{
    if(a<=b)
        return a;
    return b;
}
int max(int a, int b)
{
    if(a>=b)
        return a;
    return b;
}
double min(double a, double b)
{
    if(a<=b)
        return a;
    return b;
}
double max(double a, double b)
{
    if(a>=b)
        return a;
    return b;
}

int find(int s, int c)
{
    int p = sqrt(s+0.0);
    while(p + p*p >= s)
        p--;
    if(c > p)
        return s-c;
    else return find(p, c);
}
int main()
{
    int count=1;
    int n;
    while(scanf("%d", &n) && n)
    {
        int ans=0;
        int s, c;
        while(n--)
        {
            scanf("%d%d",&s,&c);
            ans ^= find(s, c);
        }
        printf("Case %d:\n",count++);
        if(ans)
            printf("Yes\n");
        else printf("No\n");
    }
}

努力努力...

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值