ZOJ 3702 Gibonacci number

来源:互联网 发布:linux du显示行号 编辑:程序博客网 时间:2024/05/22 12:21

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 forG(1) is a random integert, (t>=1). Given thei-th Gibonacci number valueG(i), and the numberj, your task is to output the value forG(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 contains3 integersi, G(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 fort, output -1.

Sample Input

41 1 23 5 43 4 612 17801 19

Sample Output

28-1516847

把每个数拆分成xg1+yg0  g0=1然后就求得g1了

#include <stdio.h>struct pp{    int a0,a1;}a[30];int main(){    a[0].a0=1;    a[0].a1=0;    a[1].a0=0;    a[1].a1=1;    for(int i=2;i<=20;i++)    {        a[i].a0=a[i-1].a0+a[i-2].a0;        a[i].a1=a[i-1].a1+a[i-2].a1;    }    int cas;    long long k,l,m,temp;    double temp1;    scanf("%d",&cas);    while(cas--)    {        scanf("%lld %lld %lld",&k,&m,&l);        temp1=(double)(m-a[k].a0)/a[k].a1;        if(temp1!=(int)temp1 || temp1<1)        {            printf("-1\n");            continue;        }        temp=(long long)temp1;        printf("%lld\n",temp*a[l].a1+a[l].a0);    }    return 0;}


0 0