Fibonacci again and again

Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 67 Accepted Submission(s): 45

Problem Description
任何一个大学生对菲波那契数列(Fibonacci numbers)应该都不会陌生,它是这样定义的:
F(1)=1;
F(2)=2;
F(n)=F(n-1)+F(n-2)(n>=3);
所以,1,2,3,5,8,13……就是菲波那契数列。
在HDOJ上有不少相关的题目,比如1005 Fibonacci again就是曾经的浙江省赛题。
今天,又一个关于Fibonacci的题目出现了,它是一个小游戏,定义如下:
1、  这是一个二人游戏;
2、  一共有3堆石子,数量分别是m, n, p个;
3、  两人轮流走;
4、  每走一步可以选择任意一堆石子,然后取走f个;
5、  f只能是菲波那契数列中的元素(即每次只能取1,2,3,5,8…等数量);
6、  最先取光所有石子的人为胜者;

假设双方都使用最优策略,请判断先手的人会赢还是后手的人会赢。
 

Input
输入数据包含多个测试用例,每个测试用例占一行,包含3个整数m,n,p(1<=m,n,p<=1000)。
m=n=p=0则表示输入结束。
 

Output

            如果先手的人能赢,请输出“Fibo”,否则请输出“Nacci”,每个实例的输出占一行。
 

Sample Input
1 1 1
1 4 1
0 0 0
 

Sample Output
Fibo
Nacci

思路:首先,这道题与往常的题不一样的是,这个有三堆,所以不能再用往常的 P、N分析了,于是就在网上百度啊百度啊,sg函数要看死人啊。

来吧:先发一下链接

http://lolita-angel.diandian.com/post/2012-10-11/40039176977

http://www.cnblogs.com/AndreMouche/archive/2011/03/27/1997174.html

http://www.cnblogs.com/loveidea/archive/2013/04/17/3027373.html

http://hi.baidu.com/tkdsheep/item/cad712ac817599d35af19177

http://blog.csdn.net/shuimu12345678/article/details/7677043

http://blog.sina.com.cn/s/blog_83d1d5c70100y9yd.html(这篇写的很好)

http://acm.hdu.edu.cn/forum/read.php?fid=9&tid=10617(这篇对于异或部分讲得极好!!很值得一看)

http://www.cnitblog.com/weiweibbs/articles/42735.html(太高深了,看不懂)

http://blog.sina.com.cn/s/blog_6ec19c780100vibi.html

http://hi.baidu.com/king___haha/item/542a071140107f9598ce337c

http://blog.csdn.net/qiankun1993/article/details/6765688

http://www.cnblogs.com/exponent/articles/2141477.html(这篇讲的也非常好)

准确来说,sg函数就是对于每一个状态 x,它的sg函数是x连接的点 y1,y2,y3,y4,..,yn 他们的sg值sg[y1],sg[y2],sg[y3],sg[y4],..,sg[yn] 这些值中没有出现的最小非负整数 那么对于一个P点,它的出度为0,那么sg函数一定为0, 对于一个N点,它必定能走到一个P点,而P点的sg值为0,所以N点的sg值不为零。

为啥要把值异或起来我搞懂了,但是我没明白为啥要把sg函数异或起来,而不是异或其他的函数的值。 = =||

 1 #include <cmath>
 2 #include <cstdio>
 3 #include <algorithm>
 4 #include <cstring>
 5 #include <string>
 6 #include <cstdlib>
 7 using namespace std;
 8 
 9 const int maxn=1110;
10 const int b[]={0,1,2,3,5,8,13,21,34,55,89,144,233,377,610,987};
11 int a[4],cnt;
12 bool f[maxn],flag;
13 int sg[maxn];
14 
15 void close()
16 {
17 exit(0);
18 }
19 
20 
21 void init()
22 {24     for (int i=1;i<=1000;i++)
25     {
26         memset(f,false,sizeof(f));
27         for (int j=1;j<=15;j++)
28         {
29             if (b[j]>i) break;
30             f[sg[i-b[j]]]=true;
31         }
32         for (int j=0;j<=1000;j++)
33             if (not f[j])
34             {
35                 sg[i]=j;
36                 break;
37             }
38     }
39     /*
40     for (int i=1;i<=1000;i++)
41         if (sg[i]==0)
42             printf("%d ",i);
43             */
44     cnt=0;
45     while(scanf("%d %d %d",&a[1],&a[2],&a[3])!=EOF)
46     {
47         if (a[1]==0 && a[2]==a[1] && a[3]==0)
48             break;
49         if ((sg[a[1]]^sg[a[2]]^sg[a[3]])!=0)
50             printf("Fibo\n");
51         else
52             printf("Nacci\n");
53     }
54 }
55 int main ()
56 {
57     init();
58     close();
59     return 0;
60 }

 

posted on 2013-07-22 10:30  cssystem  阅读(367)  评论(0编辑  收藏  举报