HDU - 2177

题目意思:

两堆石头,分别有n,m个石头,你先手,问在你和对手都精确思考的情况下,谁会赢?你赢,输出1,并且要你输出在取完石子之后的所有奇异局势。否则输出0

 

解题思路:

我和网上分类讨论的解法不同,我用的是二分(枚举)+hash

二分-分为了三种情况,hash用来保证所有的奇异局势不重复。

 

以下是AC代码:

#include<bits/stdc++.h>
#define register int rint
#define INF 0x3f3f3f3f3f
#define MOD 1000000007
#define mem(a,b) memset(a,b,sizeof(a))
#define PI 3.141592653589793
using namespace std;
typedef long long ll;
typedef pair<int,int>PII;
const int N=1e6+20;
const int Max=1e4+20;
const double esp=1e-8;
inline int rd() {
    char c = getchar(); int x = 0, f = 1;
    while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
    while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
    return x * f;
}
int t,a,b;
const double tmp=(1+sqrt(5.0))/2;
int erfen(int p)
{
    int l,r,mid;
    l=1;
    if(p==2)  r=b;
    else if(p==1)   r=N;
    else if(p==3)   r=a;
    while(l<=r)
    {
        mid=r+(l-r)/2;
        if(p==1)
        {
            if(a<(int)(tmp*(mid-a)))  r=mid-1;
            else    l=mid+1;
        }
        else if(p==2)
        {
            if(mid<=(int)(tmp*(a-mid)))  l=mid+1;
            else r=mid-1;
        }
        else if(p==3)
        {
            if(mid<=(int)(tmp*(b-mid))) l=mid+1;
            else r=mid-1;
        }
    }
    return r;
}
set<ll>has;
inline ll hash_mod(int x,int y)
{
    ll tmp=x*17%MOD+y*13%MOD;
    return tmp;
}
int main()
{
    while(cin>>a>>b&&(a+b))
    {
        has.clear();
        if(a>b) swap(a,b);
        if(a==int((b-a)*tmp))   cout<<0<<endl;
        else
        {
            cout<<1<<endl;
            if(a==b)    {cout<<0<<' '<<0<<endl;has.insert(0);}//第一种情况
            int x=b-a;
            if(a>(int)(tmp*x))
            {
                if(has.find(0)==has.end())
                {
                    cout<<(int)(tmp*x)<<' '<<(int)(tmp*x)+x<<endl;
                    has.insert(hash_mod((int)(tmp*x),(int)(tmp*x)+x));
                }
            }//第五种情况
            int p1=erfen(1);
            int p2=erfen(2);
            int p3=erfen(3);
            //cout<<p1<<' '<<p2<<endl;
            if(a==(int)((p1-a)*tmp)&&p1<b)
            {
                ll tmp=hash_mod(a,p1);
                if(has.find(tmp)==has.end())
                {
                    cout<<a<<' '<<p1<<endl;//第二种情况
                    has.insert(tmp);
                }
            }
            if(p2==(int)((a-p2)*tmp)&&p2<a)
            {
                ll tmp=hash_mod(p2,a);
                if(has.find(tmp)==has.end())
                {
                    cout<<p2<<' '<<a<<endl;//第三种情况
                    has.insert(tmp);
                }
            }
            if(p3==(int)((b-p3)*tmp)&&p3<a)
            {
                ll tmp=hash_mod(p3,b);
                if(has.find(tmp)==has.end())
                    cout<<p3<<' '<<b<<endl;//第四种情况
            }
        }
    }
}

 

备注:

强烈推荐看一下:http://acm.hdu.edu.cn/discuss/problem/post/reply.php?action=support&postid=22872&messageid=1&deep=0

是这个清晰的思路启发了我

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值