hdu_1848 Fibonacci again and again (SG)

Source: Internet
Author: User

Problem Description:
Problem Description
Any college student should not be unfamiliar with the Fibonacci (Fibonacci numbers), which is defined as:
F (1) = 1;
F (2) = 2;
F (n) =f (n-1) +f (n-2) (n>=3);
So, 1,2,3,5,8,13 ... is the Fibonacci series.
There are a lot of related topics in hdoj, such as 1005 Fibonacci again is once the Zhejiang province game problem.
Today, another topic about Fibonacci appears, it is a small game, defined as follows:
1, this is a two-person game;
2, a total of 3 piles of stones, the number is M, N, p;
3, two people take turns;
4, each step can choose any heap of stones, and then take away F;
5, F can only be the element in the Fibonacci sequence (that is, each time only take 1,2,3,5,8 ... Quantity);
6, the first to take the light of all the stones of the person who wins;

Assuming both sides use the optimal strategy, judge whether the person who wins the game will win or the person who will win.

Input
The input data contains multiple test cases, one row for each test case, and 3 integer m,n,p (1<=m,n,p<=1000).
M=n=p=0 indicates the end of the input.

Output

        If the initiator can win, please output "Fibo", otherwise output "Nacci", the output of each instance takes up one row.

Sample Input
1 1 1
1 4 1
0 0 0

Sample Output
Fibo
Nacci

Thinking Analysis:
This question is quite good.
The entry question for the SG function.
The MEX (minimal excludant) operation is defined first, which is an operation applied to a set that represents the smallest non-negative integer that does not belong to this set. such as Mex{0,1,2,4}=3, Mex{2,3,5}=0, mex{}=0.

For a given, forward-free graph, define the Sprague-grundy function g for each vertex of the graph as follows: g (x) =mex{g (y) | Y is the successor of X, where G (x) is sg[x]

For example: Take the stone problem, there are 1 heap n stone, at a time can only take {1,3,4} a stone, first take the stone winner, then the number of the SG value is how much.

sg[0]=0,f[]={1,3,4},

X=1, you can take away 1-f{1} stones, the remaining {0}, mex{sg[0]}={0}, so sg[1]=1;

x=2, you can take away 2-f{1} stones, the remaining {1}, mex{sg[1]}=mex{1}, so sg[2]=0;

X=3, you can take away 3-f{1,3} stones, the remaining {2,0}, mex{sg[2],sg[0]}=mex{0,0}, so sg[3]=1;

X=4, you can take away 4-f{1,3,4} stones, the remaining {3,1,0}, Mex{sg[3],sg[1],sg[0]}=mex, {1,1,0}, so sg[4]=2;

X=5, you can take away 5-f{1,3,4} stones, the remaining {4,2,1}, mex{sg[4],sg[2],sg[1]}=mex{2,0,1}, so sg[5]=3;

AC Code:

 #include <bits/stdc++.h> using namespace std; int sg[1001]; int fib[101]; int hashh[101];
    void Init () {fib[1]=1;
    fib[2]=2;
    int top=2;
    while (fib[top]<1001) {fib[++top]=fib[top-2]+fib[top-1];
}//cout<<top<<endl;
    } void Get_sg () {init ();
    int i,j,k;
    sg[0]=0;
        for (i=1;i<1001;i++) {memset (hashh,0,sizeof (HASHH)); for (j=1;
        fib[j]<=i;j++) {hashh[sg[i-fib[j]]]=1;
            } for (k=0;k<1001;k++) {if (hashh[k]) continue;
            Sg[i]=k;
        Break
    }}} int main () {Get_sg ();
    int n,m,q; while (cin>>n>>m>>q && n+m+q) {if (Sg[n]^sg[m]^sg[q]) cout<< "Fibo" &
        lt;<endl;
    else cout<< "Nacci" <<endl;
} 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.