Gibonacci number

本文介绍了一种类似于斐波那契数列的Gibonacci数列问题,通过给定特定位置的数值来逆向求解初始随机整数,并进一步计算数列中任意位置的值。文章提供了一个具体的C++实现示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Gibonacci number

 

Time Limit: 2 Seconds      Memory Limit: 32768 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 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

 

思路:设第二位为t,可以知道第i为得公式,然后算出t,后面就是fibbonacci的计算方法

#include<iostream>
#include<stdio.h>
using namespace std;
int main()
{
	int t;
	cin>>t;
	while(t--){
		int a,b,c,i,T;
		cin>>a>>b>>c;
		unsigned long long s1[23],s2[23];
		s1[0]=1;	s1[1]=0;
		s2[0]=0;	s2[1]=1;
		for(i=2;i<=20;i++){
			s1[i]=s1[i-1]+s1[i-2];
			s2[i]=s2[i-1]+s2[i-2];
		}
		if(a==1)
			T=b;
		else
			if((b-s1[a])%s2[a]==0)
				T=(b-s1[a])/s2[a];
			else
				T=-1;
		unsigned long long f[23];
		f[0]=1;
		f[1]=T;
		if(T<1)
			cout<<"-1"<<endl;
		else
		{
			for(i=2;i<=c;i++)
				f[i]=f[i-1]+f[i-2];
			//cout<<f[c]<<endl;
			printf("%lld\n",f[c]);
		}
		
	}
	return 0;
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值