hdu 2516 取石子游戏 博弈 斐波那契数

取石子游戏

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3381    Accepted Submission(s): 1981


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
ECJTU 2008 Autumn Contest



找规律打表 2 3 4 5 6 7 8 9 10

                     NNP N P PNP  P

发现斐波那契数上的状态都会输。 对于大于fac[i](斐波那契数列)

小于fac[i+1],n-fac[i]又是一个一样的博弈,但是性质一样。所以算n有几个斐波那契数组成即可


#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;

long long fac[50];

int main(){
    fac[0] = 1;
    fac[1] = 2;
    for(int i = 2;i < 50;i++){
        fac[i] = fac[i-1]+fac[i-2];
    }
    int n;
    while(scanf("%d",&n),n){
        int t = 0;
        while(n){
            int i = upper_bound(fac,fac+50,n) - fac;
            n -= fac[i-1];
            t++;
        }
        if(t % 2 == 0) printf("First win\n");
        else printf("Second win\n");
    }
    return 0;
}


你可能感兴趣的:(HDU,HDU,博弈,2516)