HDU2516 Stone Game (Fibonacci game) __ The Fibonacci game

Source: Internet
Author: User
Problem Description

1 heaps of stones have N, two people take turns. The first person can take any number of the 1th time, but not all of them. The number of stones to be taken at a later time cannot exceed twice times of the last number of children. Take the winner. Negative output of the first Second
Win "first win" wins the output. Input

There are multiple groups entered. The 1th line of each group is 2<=n<2^31. N=0 exit. Output

First take the negative output "Second win". First win is the output of the winner. Refer to Sample Output. Sample Input

2
10000
0
Sample Output
Second win
Second win-A-win

train of Thought

This is a Fibonacci ' s Game (Fibonacci game)
The Fibonacci game model, in general, is this:
There's a bunch of rocks with numbers n, the game both sides take turns to get the stone, satisfies: the first time cannot take all the stones to finish, after each can take the number of stones between 1 to the opponent just takes the number of stones twice times (contains 1 and the opponent just take the number of stones twice times). Agreed to take the last stone for the winner, the inevitable failure.

Analysis:
n = 2 o'clock output second;
n = 3 o'clock is also output second;
n = 4 o'clock, the first person wants to win must take 1 first, then the remaining number of stones is 3, at this time, no matter how the second person to take, the first person can win, the output of primary;
n = 5 o'clock, first can not win, because he took the 2 o'clock, second directly take off the remaining 3 will win, when he took 1 o'clock, this becomes the case of N 4, so the output is second;
n = 6 o'clock, first, if you remove 1, you can turn the situation into a case of n 5, so it is first;
n = 7 o'clock, first takes off 2, the situation turns N to 5, so first wins, so first is the output;
n = 8 o'clock, when the 1 is taken, the situation becomes 7 situation, the second person can win, first take 2, the situation becomes n 6 get the case, is also the second person to win, take 3, second directly remove 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, the point of failure, careful people will find these meet the Fibonacci law, you can infer that 13 is also a must lose point.
With the help of "Zeckendorf theorem" (Ziekendorf theorem): Any positive integer can be expressed as the sum of several discontinuous Fibonacci numbers. N=12, as long as who can make the stone left 8 and this pick son not more than 3 can win. Therefore, 12 can be regarded as 8+4, 8 as a station, equivalent to the 4 "asthma operation." And such as 13,13=8+5,5 is the inevitable failure of the state, that 13 is also a must-lose state. In other words, as long as the Fibonacci number is a must-lose point.
So we can use the Fibonacci formula: Fib[i] = fib[i-1] + fib[i-2], as long as n is the Fibonacci number to output second.

So the conclusion is: as long as the number of stones is the Fibonacci number, then the winning code

 #include <bits/stdc++.h> using namespace std; int f[100]; void init () {f[0]=2;
    f[1]=3;
for (int i=2; i<45; i++) f[i]=f[i-1]+f[i-2];
    int main () {init ();
    int n;
        while (scanf ("%d", &n) &&n) {int flag=1;
                for (int i=0; i<45; i++) if (f[i]==n) {flag=0;
            Break
        if (flag) puts ("A-win");
    Else puts ("Second win");
return 0; }

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.