HDU 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
 
Source
 

解题思路:

这题没法用sg直接求了,数据量太大了,并且sg会受上次的影响,所以不一定。

因此,仅仅能打表找规律,找到规律发现假设满足斐波那契数列 f[n]=f[n-1]+f[n-2] 的数列,Second Win 否则 ,First Win

解题代码:

#include <iostream>
#include <cstdio>
#include <set>
using namespace std; set <int> mys; void ini(){
int f1=1,f2=1,f3=2;
while(true){
f3=f1+f2;
if(f3<=0) break;
mys.insert(f3);
f1=f2;
f2=f3;
}
} int main(){
ini();
int n;
while(scanf("%d",&n)!=EOF && n!=0){
if(mys.find(n)!=mys.end()) printf("Second win\n");
else printf("First win\n");
}
return 0;
}
上一篇:poj 1067 取石子游戏( 威佐夫博奕)


下一篇:HDU - 4994 Revenge of Nim (取石子游戏)