ZOJ3702-Gibonacci number

120 篇文章 1 订阅

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

Author:  YU, Zhi
Contest:  The 13th Zhejiang University Programming Contest


题意:已知一串正整数中g[0]=1,告诉你g[i]的值,求g[j],若无解输出-1

解题思路:设g[1]=x,然后推出后面几项即可,会发现常数项和x的系数是斐波那契数列


#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <vector>
#include <set>
#include <stack>
#include <map>
#include <climits>

using namespace std;

#define LL long long
const int INF=0x3f3f3f3f;

int a[30],b[30];

void init()
{
    a[0]=0,a[1]=1;
    b[0]=1,b[1]=0;
    for(int i=2;i<=22;i++)
        a[i]=a[i-1]+a[i-2],b[i]=b[i-1]+b[i-2];
}

int main()
{
    init();
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int x,y,z;
        scanf("%d%d%d",&x,&y,&z);
        y-=b[x];
        if(y%a[x]!=0||y<=0) {printf("-1\n");continue;}
        int xx=y/a[x];
        printf("%lld\n",1LL*xx*a[z]+b[z]);
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值