B - 取(2堆)石子游戏

有两堆石子,数量任意,可以不同。游戏开始由两个人轮流取石子。游戏规定,每次有两种不同的取法,一是可以在任意的一堆中取走任意多的石子;二是可以在两堆中同时取走相同数量的石子。最后把石子全部取完者为胜者。现在给出初始的两堆石子的数目,如果轮到你先取,假设双方都采取最好的策略,问最后你是胜者还是败者。如果你胜,你第1次怎样取子?

Input

输入包含若干行,表示若干种石子的初始情况,其中每一行包含两个非负整数a和b,表示两堆石子的数目,a和b都不大于1,000,000,且a<=b。a=b=0退出。

Output

输出也有若干行,如果最后你是败者,则为0,反之,输出1,并输出使你胜的你第1次取石子后剩下的两堆石子的数量x,y,x<=y。如果在任意的一堆中取走石子能胜同时在两堆中同时取走相同数量的石子也能胜,先输出取走相同数量的石子的情况.

Sample Input

1 2
5 8
4 7
2 2
0 0

Sample Output

0
1
4 7
3 5
0
1
0 0
1 2

威佐夫博奕:只有当x=k*(sqrt(5)+1)/2,y=x+k,的时候才会输,否则就将赢;先考虑两个相差多少,可以取同时取两边让其变成相差的的k,然后在考虑取一边的

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<cmath>
#include<string.h>
#include<algorithm>
#define sf scanf
#define pf printf
#define pb push_back
#define mm(a,b) memset((a),(b),sizeof(a))
#include<vector>
typedef long long ll;
typedef long double ld;
const ll mod=1e9+7;
using namespace std;
const double pi=acos(-1.0);
void chuli(int n,int m)
{
    if(n==0||m==0)
    {
        pf("1\n0 0\n");
        return ;
    }
    int qq=(int)(m-n)*1.618;
    if(qq==n)
    {
            pf("0\n");
        return ;
    }
        double cas=(sqrt(5)+1)/2;
        pf("1\n");
        if(n==m)
            pf("0 0\n");
        int d=m-n;
        if(d!=0)
        if(d*cas<n)
        {
            int x=(int)d*cas,y=(int)d*(cas+1);
             pf("%d %d\n",x,y);
        }
        for(int i=1;;i++)
        {
            int x=(int)i*cas,y=(int)i*(cas+1);
            if(x==n)
            {
                if(m>y)
                pf("%d %d\n",x,y);
            }
            if(y==n)
            {
                if(m>x)
                pf("%d %d\n",x,y);
            }
            if(y==m)
            {
                if(n>x)
                pf("%d %d\n",x,y);
            }
            if(x==m)
            {
                if(n>y)
                pf("%d %d\n",x,y);      
            }
            if(x>m)
            return ;
        }
}
int main()
{
    int n,m;
    while(1)
    {
        sf("%d%d",&n,&m);
        if(n==0&&m==0) return 0;
        chuli(n,m);
    }
}

转载于:https://www.cnblogs.com/wzl19981116/p/9367394.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值