hdu2516—取石子游戏(斐波拉契博弈)

来源:互联网 发布:mkv播放器 for mac 编辑:程序博客网 时间:2024/06/07 10:15

题目链接:传送门

取石子游戏

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5343    Accepted Submission(s): 3233


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
213100000
 

Sample Output
Second winSecond winFirst win
 

斐波拉契博弈结论:后手胜当n是斐波拉契数时


#include <iostream>#include <cstdio>#include <cstring>#include <string>#include <stack>#include <queue>#include <algorithm>#include <cmath>using namespace std;typedef long long ll;typedef pair<int,int>PA;const int N = 1009;const int M = 30009;const int INF = 0x3fffffff;int febo[50];void init(){    febo[0] = febo[1] = 1;    for( int i = 2 ; i < 50 ; ++i ){        febo[i] = febo[i-1]+febo[i-2];    }}int main(){    int n;    while( ~scanf("%d",&n)&&n ){        init();        int flag = 0;        for( int i = 0 ; i < 50 ; ++i ){            if( febo[i] == n ) flag = 1;        }        if( flag ) printf("Second win\n");        else printf("First win\n");    }    return 0;}


原创粉丝点击