现在的位置: 首页 > 综合 > 正文

HDU 2516 取石子游戏 (博弈论)

2014年10月31日 ⁄ 综合 ⁄ 共 726字 ⁄ 字号 评论关闭

取石子游戏

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;
}

抱歉!评论已关闭.