HDU - 2516 取石子游戏(斐波那契)

来源:互联网 发布:淘宝开店类目如何选择 编辑:程序博客网 时间:2024/06/07 21:49

题目:

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

这个博弈俗称斐波那契博弈,简称FIB博弈。

原理到处都有解释,我就不扯了,不过里面用到的核心定理,我有解释:斐波那契数列的齐肯多夫定理

代码:

#include<iostream>#include<stdio.h>using namespace std;int list[46];int main(){list[1] = 1;list[2] = 2;for (int i = 3; i <= 45; i++)list[i] = list[i - 1] + list[i - 2];int n;while (cin >> n){if (n == 0)break;int i = 2;for (; i <= 45; i++)if (list[i] == n){cout << "Second win\n";break;}if (i > 45)cout << "First win\n";}return 0;}

0 0
原创粉丝点击