您的位置:首页 > 其它

斐波那契博弈 hdu2516 取石子游戏

2015-12-10 18:52 309 查看
传送门:点击打开链接

题意:1堆石子n个,第一个人可以取任意个数但不能全部取完,以后每次拿的个数不能超过上一次对手拿的个数的2倍,轮流拿石子,问先手是否必赢

思路:斐波那契博弈,后手赢的情况的数字会呈现斐波那契数列。

#include<map>
#include<set>
#include<cmath>
#include<ctime>
#include<stack>
#include<queue>
#include<cstdio>
#include<cctype>
#include<string>
#include<vector>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<functional>
#define fuck(x) cout<<"["<<x<<"]"
#define FIN freopen("input.txt","r",stdin)
#define FOUT freopen("output.txt","w+",stdout)
using namespace std;
typedef long long LL;

const int MX = 50;
LL F[MX];

void presolve() {
F[1] = 1; F[2] = 2;
for(int i = 3; i < 50; i++) {
F[i] = F[i - 1] + F[i - 2];
}
}

bool solve(int n) {
for(int i = 1; i < MX; i++) {
if(F[i] == n) return false;
}
return true;
}

int main() {
presolve();
int n; //FIN;
while(~scanf("%d", &n), n) {
printf("%s\n", solve(n) ? "First win" : "Second win");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: