ZOJ3702(打表)

Gibonacci number
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 3 integers i, 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 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

斐波那契的变形,推个公式,G(n)=F(n-1)*G(1)+F(n-2)
注意n取1的时候和G(n)为负值的时候,注意数组边界

#include<bits/stdc++.h>
using namespace std;
int main()
{
   
    long long i,n,t,tmp1,tmp2,tmp3,b,c,d;
    long long a[100];
    a[0]=1;
    a[1]=1;
    a[2]=2;
    for(i=3; i<=40; i++)
        a[i]=a[i-1]+a[i-2];
    scanf("%lld",&t);
    while(t--)
    {
   
        scanf("%lld%lld%lld",&tmp1,&tmp2,&tmp3);
        b=1;
        c=0;
        d=0;
        if(tmp1==1)
        {
   
            c=tmp2;
            if(tmp3==1)
                cout<<c<<'\n';
            else
            {
   
                d=a[tmp3-1]*c+a[tmp3-2];
                cout<<d<<'\n';
            }
        }
        else
        {
   
            c=(tmp2-a[tmp1-2])/a[tmp1-1];
            if(c*a[tmp1-1]!=tmp2-a[tmp1-2]||c<=0)
            {
   
                cout<<"-1"<<'\n';
            }
            else
            {
   
                if(tmp3==1)
                    cout<<c<<'\n';
                else
                {
   
                    d=a[tmp3-1]*c+a[tmp3-2];
                    cout<<d<<'\n';
                }
            }
        }
    }
    return 0;
}

全部评论

相关推荐

lingo12:1.最好加个业务项目,大部分面试官工作以后会更偏重业务 2.实习部分描述一般般,可能hr看到会觉得你产出不够不给你过简历 3.蓝桥杯这些大部分人都有的,不如不写,反而减分项。
点赞 评论 收藏
分享
头像
02-15 16:23
中南大学 Java
野猪不是猪🐗:签了美团真是不一样! 亲戚们都知道我签了美团,过年都围着我问送一单多少钱,还让弟弟妹妹们引以为戒,笑我爸我妈养了个🐢孩子,说从小就知道我这个人以后肯定没出息,我被骂的都快上天了
点赞 评论 收藏
分享
评论
2
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务