码迷,mamicode.com
首页 > 其他好文 > 详细

HDU 2516 取石子游戏(巴什博弈)

时间:2014-05-08 15:47:51      阅读:370      评论:0      收藏:0      [点我收藏+]

标签:hdu   2516   巴什博弈   

取石子游戏

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


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
 

题意明确,此题也是从PN图入手,如下:

2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20……

p p n p n n p n  n    n   n   p    n   n   n   n    n   n   n……

以上PN图可以很容易画出来,必败状态为2,3,5,8,13.。。。其实我没画21时的状态,因为看到必败状态序列是菲波那切数列,所以就直接猜想必败状态符合斐波那契,提交后竟然AC了。。。。

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <climits>

using namespace std;

int a,b;

int main(){
    //freopen("in.txt","r",stdin);
    //(author : CSDN iaccepted)
	int n,ts;
	bool mark;
	
	
	while(scanf("%d",&n) && n){
		a = 2,b = 3;
		mark = false;
		while(a<=n){
			if(a==n || b==n){
				mark = true;
				break;
			}
			ts = (a  + b);
			a = b;
			b = ts;
		}
		if(mark){
			printf("Second win\n");
		}else{
			printf("First win\n");
		}
	}
    return 0;
}



HDU 2516 取石子游戏(巴什博弈),布布扣,bubuko.com

HDU 2516 取石子游戏(巴什博弈)

标签:hdu   2516   巴什博弈   

原文地址:http://blog.csdn.net/iaccepted/article/details/25301057

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!