The ACM-SG function of maid -- hdu1848

Source: Internet
Author: User

Fibonacci again and againTime Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission (s): 4248 Accepted Submission (s): 1768

Problem Description any college student should be familiar with the Fibonacci series (Fibonacci numbers), which is defined as follows:
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 many related questions on HDOJ. For example, 1005 Fibonacci again is the competition question of Zhejiang Province.
Today, another question about Fibonacci emerged. It is a small game and its definition is as follows:
1. This is a two-person game;
2. There are 3 piles of stones in total. The numbers are m, n, and p;
3. The two take turns;
4. You can select any pile of stones for each step, and then remove f stones;
5. f can only be an element in the Fibonacci series (that is, each time only 1, 2, 3, 5, 8... );
6. The winner is the first person to take all stones;

If both parties adopt the optimal strategy, determine whether the first-hand person will win or the latter-hand person will win.
The Input data contains multiple test cases. Each test case occupies one row and contains three integers, m, n, p (1 <= m, n, p <= 1000 ).
M = n = p = 0 indicates that the input ends.
Output: if the first player can win the game, Output "Fibo"; otherwise, Output "Nacci". The Output of each instance occupies one line.
Sample Input

1 1 11 4 10 0 0
Sample Output
FiboNacci
Authorlcy SourceACM Short Term Exam_2007/12/13 Question: http://acm.hdu.edu.cn/showproblem.php? Pid = 1, 1848
The first question with the SG function. I still don't know much about SG functions. However, I can see the following:

First define the mex (minimal excludant) operation, which is applied to a set operation, indicating the smallest non-negative integer that does not belong to this set.

For example, mex {0, 1, 2, 4} = 3, mex {2, 3, 5} = 0, and mex {} = 0.


For a given directed acyclic graph, defineSD-GrundyFunction g is as follows: g (x) = mex {g (y) | y is the successor of x}. Here g (x) is sg [x]


For example, if there are 1 pile of n stones, only {1, 3, 4} stones can be taken at a time. What is the SG value of each number when the stone winner is obtained first?

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


When x = 1, 1-f {1} stones can be taken away, with {0} remaining stones and mex {sg [0] }={ 0} remaining. Therefore, sg [1] = 1;

When x = 2, 2-f {1} stones can be taken away, with {1} remaining stones and mex {sg [1] }={ 1} remaining. Therefore, sg [2] = 0;

When x = 3, 3 f {1, 3} stones can be taken away, with {2, 0} remaining stones, mex {sg [2], sg [0] }={ 0, 0 }, therefore, sg [3] = 1;

When x = 4, 4-f {, 4} stones can be taken away, with {, 0} remaining stones, mex {sg [3], sg [1], sg [0] }={ 1, 1, 0}, so sg [4] = 2;

When x = 5, 5-f {, 4} stones can be taken away, with {, 1} remaining stones, mex {sg [4], sg [2], sg [1] }={ 2, 0, 1}, so sg [5] = 3;

And so on .....

X 0 1 2 3 4 5 6 7 8 ....

Sg [x] 0 1 0 1 2 3 2 0 1...


Calculate the SG value from the range of 1-n.

F (number of steps that can be stored. f [0] indicates the number of steps that can be performed)

F [] needs to be sorted in ascending order

1. The number of optional steps is 1 ~ The continuous integer of m, which can be directly modulo, SG (x) = x % (m + 1 );

2. The number of optional steps is any step, SG (x) = x;

3. The number of optional steps is a series of discontinuous numbers, calculated using GetSG ().


The above is an explanation of the creation of the SG template from jumping_frog blog. I will also create a template for the SG function later.


With the above method, this question is simple. First, create an f array, that is, a series of Fibonacci. Then, pre-process the SG array of less than 1000 and use the template:
// Obtain the SG Array Function template. t indicates the number of f arrays, and n indicates the maximum number of sg arrays required. // f indicates the number of arrays that can be obtained. (For this question, it is the Fibonacci series. // sometimes, if t is known, the parameter void get_sg (int t, int n) {int I, j; memset (sg, 0, sizeof (sg) is not required )); for (I = 1; I <= n; I ++) {memset (mex, 0, sizeof (mex); // for g (x) 1 for (j = 1; j <= t & fib [j] <= I; j ++) mex [sg [I-fib [j] = 1; // find the minimum number that does not belong to the set for (j = 0; j <= n; j ++) if (! Mex [j]) break; sg [I] = j ;}}


Many SG questions can be considered as multiple Nim games. Then we can analyze the singular state and the non-singular state to determine the answer. About, Nim game visible blog: http://blog.csdn.net/lttree/article/details/24874819
The complete code for this question is as follows:
/*************************************** **************************************** * ******************* Author: tree ** From: http://blog.csdn.net/lttree ** Title: Fibonacci again and again ** Source: hdu 1848 ** Hint: SG *************************************** **************************************** * *******************/# include
 
  
# Include
  
   
Int fib [21]; // fib stores the Fibonacci series int sg [1001]; // sg [] to save the SG value bool mex [1001]; // mex {} // construct the SG array. For the meanings of each function step, see the preceding template void get_sg (int n) {int I, j; memset (sg, 0, sizeof (sg); for (I = 1; I <= n; I ++) {memset (mex, 0, sizeof (mex); for (j = 1; fib [j] <= I; j ++) mex [sg [I-fib [j] = 1; for (j = 0; j <= n; j ++) if (! Mex [j]) break; sg [I] = j ;}} int main () {int I, m, n, p; // construct the fib onacci series fib [0] = 1, fib [1] = 1; for (I = 2; I <21; ++ I) fib [I] = fib [I-1] + fib [I-2]; // pre-processing get sg array get_sg (1000); while (scanf ("% d ", & m, & n, & p) & m + n + p) {if (sg [m] ^ sg [n] ^ sg [p]) = 0) printf ("Nacci \ n"); else printf ("Fibo \ n");} return 0 ;}
  
 


Related Article

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.