题目
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
这道题目只要列出一下先手输掉的情况2,3,5,8,13....................这样不难发现这是一个斐波那契博弈,也就是说,只要是斐波那契数,就是先手必败!
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <vector>
#include <string.h>
#include <string>
typedef long long ll;
#define INF 0x3f3f3f3f
using namespace std;
int a, b;
int main() {
int n, m;
bool flag;
while (scanf("%d", &n) && n) {
a = 2, b = 3;
flag = false;
while (a <= n) {
if (a == n || b == n) {
flag = true;
break;
}
m = (a + b);
a = b;
b = m;
}
if (flag) printf("Second win\n");
else printf("First win\n");
}
return 0;
}