HDU2516:取石子游戏(斐波那契博弈)

Problem Description
1堆石子有n个,两人轮流取.先取者第1次可以取任意多个,但不能全部取完.以后每次取的石子数不能超过上次取子数的2倍。取完者胜.先取者负输出"Second win".先取者胜输出"First win".
 

Input
输入有多组.每组第1行是2<=n<2^31. n=0退出.
 

Output
先取者负输出"Second win". 先取者胜输出"First win".  
参看Sample Output.
 

Sample Input
   
   
   
   
2 13 10000 0
 

Sample Output
   
   
   
   
Second win Second win First win


 

典型的斐波那契博弈

有关斐波那契博弈可以参考这篇博文:http://blog.csdn.net/dgq8211/article/details/7602807

 

#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;

__int64 a[50],len;
const __int64 inf = 2147483648+10;

int main()
{
    int i,j;
    __int64 n;
    a[1] = 1;
    a[2] = 1;
    for(i = 3; i<=1000000; i++)
    {
        a[i] = a[i-1]+a[i-2];
        if(a[i]>=inf)
            break;
    }
    len = i;
    while(~scanf("%I64d",&n),n)
    {
        int flag = 0;
        for(i = 1; i<len; i++)
        {
            if(a[i] == n)
            {
                flag = 1;
                break;
            }
            if(a[i]>n)
                break;
        }
        if(flag)
            printf("Second win\n");
        else
            printf("First win\n");
    }

    return 0;
}


 

你可能感兴趣的:(HDU,博弈)