取石子游戏
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 8756 Accepted Submission(s): 5307
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
Recommend
lcy
斐波那契博弈
直接列表找规律,发现当n为斐波那契数的时候,先手必败,打一个表直接判断即可。
具体证明见https://blog.youkuaiyun.com/acm_cxlove/article/details/7835016
#include "bits/stdc++.h"
using namespace std;
long long f[100];
int main() {
f[0]=1;f[1]=1;
long long x=(long long)1<<31;
int cnt=0;
for(int i=2;;i++)
{
f[i]=f[i-1]+f[i-2];
if(f[i]>=x)break;
cnt=i;
}
int n;
while(~scanf("%d",&n)&&n)
{
bool ok=1;
for(int i=1;i<=cnt;i++)
{
if(n==f[i])
{
ok=0;break;
}
}
if(ok)puts("First win");
else puts("Second win");
}
}