HDU 2516 取石子游戏 斐波那契博弈裸题

题目:

http://acm.hdu.edu.cn/showproblem.php?pid=2516

题意:

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

思路:

裸的斐波那契博弈,留个模板

#include 
using namespace std;

typedef long long ll;
const int N = 60;
ll fib[N];
void table()
{
    fib[0] = 0, fib[1] = 1;
    for(int i = 2; i <= 50; i++)
        fib[i] = fib[i-1] + fib[i-2];
}
int main()
{
    ll n;
    table();
    while(scanf("%lld", &n), n != 0)
    {
        bool flag = false;
        for(int i = 0; i <= 50; i++)
        {
            if(n == fib[i])
            {
                flag = true; break;
            }
            else if(n < fib[i]) break;
        }
        if(flag) printf("Second win\n");
        else printf("First win\n");
    }
    return 0;
}

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