take a stone game 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 Heap of stones There are N, two people take turns. The 1th time the first person can take any number, but not all of them. The number of stones to be taken at a later time cannot exceed twice times the number of previous fetching. The winner wins. The first to take the negative output "Second win". The first winner wins the output.
Input inputs have multiple groups. The 1th row of each group is 2<=n<2^31. N=0 exit.
Output "Second win". The first winner wins the output.
See sample Output.
Sample Input
2 13 10000 0
Sample Output
Second win Second win first win
Source ecjtu Autumn Contest
Find a regular Table 2 3 4 5 6 7 8 9 10
NNP N p PNP p
The status of Fibonacci numbers is found to be lost. For greater than Fac[i] (Fibonacci sequence)
Less than Fac[i+1],n-fac[i] is a similar game, but the same nature. So Count N has a few Fibonacci numbers to make it
#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;
}