HDU 1848 Fibonacci again and again (SG function), hdufibonacci
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission (s): 10069 Accepted Submission (s): 4289
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 Input1 1 11 4 10 0 0
Sample OutputFiboNacci
Authorlcy
SourceACM Short Term Exam_2007/12/13
Recommendlcy | We have carefully selected several similar problems for you: 1850 1849 2147 2149
Deformation of Nim games,
The only difference is that the available set is Fib {, 5 ,.....}
Then find the SG function.
#include<cstdio>#include<algorithm>#include<cstring>using namespace std;const int MAXN=1e4+10,INF=1e9+10;inline int read(){ char c=getchar();int x=0,f=1; while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();} while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();} return x*f;}int fib[MAXN];int S[MAXN],SG[MAXN];int main(){ #ifdef WIN32 freopen("a.in","r",stdin); #else #endif int N,M,P; N=20; fib[0]=1;fib[1]=1; for(int i=2;i<=20;i++) fib[i]=fib[i-1]+fib[i-2]; N=1001; for(int i=1;i<=N;i++) { memset(S,0,sizeof(S)); for(int j=1;j<=20&&fib[j]<=i;j++) S[ SG[ i-fib[j] ] ] = 1; for(int j=0;;j++) if(S[j]==0) {SG[i]=j;break;} } while(scanf("%d%d%d",&N,&M,&P)) { if(N==0&&M==0&&P==0) break; if(SG[N]^SG[M]^SG[P]) printf("Fibo\n"); else printf("Nacci\n"); } return 0;