Huawei machine Test-take stone game (advanced 160 points)

Source: Internet
Author: User

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 "Secondwin". The first person wins the output "Firstwin".

Input

The input has more than one group. The 1th row of each group is 2<=n<2^31.n=0 exit.

Output

The first to take the negative output "Secondwin". The first person wins the output "Firstwin".
See sample Output.

Sample Input

2

13

10000

0

Sample Output

Second win

Second win

First win


Ideas:

This is a Fibonacci ' s game ( Fibonacci game )

The Fibonacci game model is roughly the same:
There are a number of stones of N, the game both sides take the stones, meet:
1. The initiator cannot take all the stones for the first time;
2. The number of stones that can be taken at a later time is between 1 and twice times the number of stones the opponent has just taken (including 1 and twice times the number of stones the opponent has just taken).
Agreed to take the last stone of the man-made winner, to seek the inevitable.  Analysis:
n = 2 o'clock output second;
n = 3 o'clock is also the output second;
n = 4 o'clock, the first person wants to win must first Take 1, then the remaining stone number is 3, at this time no matter how to take the second person, the first person can win, output firstly;
n = 5 o'clock, first can not win, because he takes 2 o'clock, second directly take out the remaining 3 will win, when he takes 1 o'clock, this becomes the case of N 4, so the output is second;
n = 6 o'clock, first only remove 1, you can let the situation into the case of N 5, so the output is first;
n = 7 o'clock, first takes out 2, the situation becomes N 5, so first wins, so the output is first;
n = 8 o'clock, when first takes 1, the situation becomes 7 of the situation, the second person can win, at the first take 2, the situation becomes N 6 get the situation, is also the second person to win, take 3, second directly take out the remaining 5, so n = 8 o'clock, the output is second;

............

From the above analysis can be seen, N is 2, 3, 5, 8, these are output second, that is, must be defeated, careful people will find these meet Fibonacci number law, you can infer that 13 is also a must defeat point.

With the aid of the "Zeckendorf theorem" (Ziekendorf theorem): Any positive integer can be expressed as a sum of several discontinuous Fibonacci. N=12, as long as who can make the stone left 8 and this time take the son not more than 3 can win. Therefore, 12 can be regarded as 8+4, 8 as a station, equivalent to 4 for the "Asthma operation." Another example is that the 13,13=8+5,5 is going to fail, and that 13 is also a must-fail state. In other words, as long as the Fibonacci number, is a must lose points.

So we can use the Fibonacci number formula: Fib[i] = fib[i-1] + fib[i-2], as long as n is the Fibonacci number of the output second.

#include <iostream>using namespace Std;int main () {//calculation to f[48] is nearing the data range beyond int f[48];f[0] = 0, f[1] = 1;int I, n;for (i = 2; i < i++) f[i] = F[i-1] + f[i-2];while (cin>>n && n!=0) {BOOL flags = 0;//According to the topic meaning, n>=2, so from f[3 ] = 2 starts for (i = 3; I < i++) {if (n = = F[i]) {flags = 1;break;}} if (flags) cout<< "Second win" <<endl;else cout<< "first win" <<ENDL;} return 0;}



Huawei machine Test-take stone game (advanced 160 points)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.