【hdu 2177】取(2堆)石子游戏

Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2403 Accepted Submission(s): 1441

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

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

【题解】

/*
【威佐夫博弈】:http://blog.csdn.net/harlow_cheng/article/details/54097803
    输入的a,b满足a<=b;
    则先作差
    x = b-a;
    temp = floor((1.0+sqrt(5.0))/2.0);
    if (temp==a)
        是必败点,输出no;
    else
    {
        你所走的一步必然要让对方面临必败点;
        这里temp=n[x];
        if (a>temp)
        {(同时减去的话差值x不变,所以如果a不能变成n[x]的话就不能同时取)
            可以两堆同时减去a-n[x];
            ->(n[x],b-a+n[x]);
            ->(n[x],n[x]+x);
            ->符合题意的必败点;
            输出tmep,b-a+temp;
        }
        单独取的话;
        肯定是减少这两个中的一个;
        ①如果减少b;
            则求a对应的必败态的b是多少->b'(可以预处理得到);
            则如果b>b',就输出这个(a,b')就好;
        ②如果减少a
            则求b对应的必败态的a是多少->a'
            则如果a>a',就输出这个(a',b)就好;
    }
*/


【完整代码】

#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define rei(x) scanf("%d",&x)
#define rel(x) scanf("%I64d",&x)

typedef pair<int,int> pii;
typedef pair<LL,LL> pll;

//const int MAXN = x;
const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const double pi = acos(-1.0);

int a,b;
int dic[2000000];

int main()
{
    int now = 0,n=0,m;
    while (n<=1000000)
    {
        n = floor(now*(1.0+sqrt(5.0))/2.0);
        m = n+now;
        dic[n] = m;
        dic[m] = n;
        now++;
    }
    while (~scanf("%d%d",&a,&b))
    {
        if (a==0 && b==0)
            break;
        int x = b-a;
        int temp = floor(x*(1.0+sqrt(5.0))/2.0);
        if (a==temp)
            puts("0");
        else
        {
            puts("1");
            if (a>temp)
                printf("%d %d
",temp,temp+x);
            int judge;
            //sub b
            judge = dic[a];
            if (b>judge)
            {
                printf("%d %d
",min(a,judge),max(a,judge));
                continue;
            }
            //sub a
            judge = dic[b];
            if (a>judge)
                printf("%d %d
",min(judge,b),max(judge,b));
        }
    }
    return 0;
}
原文地址:https://www.cnblogs.com/AWCXV/p/7626726.html