ZOJ 3702 Gibonacci number【思维】

Gibonacci number

Time Limit: 2 Seconds       Memory Limit: 65536 KB

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 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


思路:递推出Gn和G0,G1相关式子:

G0=1;

G1=t;

G2=G1+G0;

G3=2*G1+G0;

G4=3*G1+2*G0;

G5=5*G1+3*G0;

则不难推出,Gi=F【i-1】*t+F【i-2】*1

如果t是正整数的话,辣么就有符合的条件,并且t=(Gi-F【i-2】*1)/F【i-1】;

如果t不是正整数的话,了么就没有符合条件,输出-1.


坑点:如果t==0的时候,也属于没有符合条件,因为题中给出t的范围是大于等于1了~

AC代码:

#include<stdio.h>
#include<string.h>
#include<math.h>
using namespace std;
#define ll long long int
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        ll f[25];
        ll g[25];
        f[0]=1;
        f[1]=1;
        for(int i=2;i<=25;i++)
        {
            f[i]=f[i-1]+f[i-2];
        }
        memset(g,0,sizeof(g));
        int i,gi,j;
        ll t;
        scanf("%d%d%d",&i,&gi,&j);
        int flag=0;
        if(i==1)
        {
            t=gi;
            flag=1;
        }
        else
        {
            if(((gi-f[i-2])%f[i-1])==0)
            t=(gi-f[i-2])/f[i-1],flag=1;
            else flag=0;
        }
        if(flag==0||t<1)
        {
            printf("-1\n");continue;
        }
        g[0]=1;
        g[1]=t;
        for(int k=2;k<=25;k++)
        {
            g[k]=g[k-1]+g[k-2];
        }
        printf("%lld\n",g[j]);
    }
}











  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值