Gibonacci number-斐波那契数列

Description

In mathematical terms, the normal sequence F(n) of Fibonacci numbers is defined by the recurrence relation

F(n)=F(n-1)+F(n-2)

with seed values

F(0)=1, F(1)=1

In this Gibonacci numbers problem, the sequence G(n) is defined similar

G(n)=G(n-1)+G(n-2)

with the seed value for G(0) is 1 for any case, and the seed value for G(1) is a random integer t(t>=1). Given the i-th Gibonacci number value G(i), and the number j, your task is to output the value for G(j)

Input

There are multiple test cases. The first line of input is an integer T < 10000 indicating the number of test cases. Each test case contains 3integers iG(i) and j. 1 <= i,j <=20, G(i)<1000000

Output

For each test case, output the value for G(j). If there is no suitable value for t, output -1.

Sample Input

4
1 1 2
3 5 4
3 4 6
12 17801 19

Sample Output

2
8
-1
516847


 1 #include"iostream"
 2 #include"algorithm"
 3 #include"cstring"
 4 #include"queue"
 5 #include"cmath"
 6 #include"cstdio"
 7 #include"cstdlib"
 8 using namespace std;
 9 #define sr(x) scanf("%d",&x)
10 #define sc(x) printf("%d",x)
11 #define hh printf("\n")
12 int main()
13 {
14     long long f[25],x,y,g,z=-1,i;
15     f[0]=1;f[1]=1;
16     for(i=2;i<21;i++)f[i]=f[i-1]+f[i-2];
17     int tt;
18     cin>>tt;
19     while(tt--)
20     {
21         cin>>x>>g>>y;
22         if(x>1)
23         {
24             if((g-f[x-2])%f[x-1]==0)z=(g-f[x-2])/f[x-1];
25             else {cout<<-1<<endl;continue;}
26         }
27         else
28         {
29             if(x==1)z=g;
30             else {cout<<-1<<endl;continue;}
31         }
32         if(z<1){cout<<-1<<endl;continue;}
33         if(y==0)cout<<1<<endl;
34         else if(y==1)cout<<z<<endl;
35         else cout<<f[y-2]+f[y-1]*z<<endl;
36     }
37     return 0;
38 }

 

posted on 2013-11-07 23:38  lveternal  阅读(295)  评论(0编辑  收藏  举报

导航