链接:
G. 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)=1In 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
思路:
坑:
code:
#include<stdio.h>
#include<string.h>
const int maxn = 1000000;
long long f[40];
void inint()
{
f[0] = f[1] = 1;
for(int i = 2; i <= 30; i++) f[i] = f[i-1]+f[i-2];
}
int main()
{
inint();
int n;
int i,j;
long long g;
scanf("%d", &n);
{
while(n--)
{
scanf("%d%lld%d", &i,&g,&j);
long long x;
if(i > 1)
{
x = (g-f[i-2])/f[i-1];
if(x*f[i-1] != (g-f[i-2]) || x < 1)
{
printf("-1\n"); continue;
}
if(j > 1) printf("%lld\n", f[j-2]+f[j-1]*x);
else printf("%lld\n", x);
}
else if(i == 1)
{
x = g;
if(x < 1)
{
printf("-1\n"); continue;
}
if(j == 1) printf("%lld\n", x);
else printf("%lld\n", f[j-2]+f[j-1]*x);
}
}
}
return 0;
}
本文探讨了一种类似于斐波那契数列的Gibonacci数列问题,通过递推公式解析并给出了高效的求解算法。针对特定输入,文章详细介绍了如何通过已知数列值逆推初始值,并计算目标位置的数列值。
822

被折叠的 条评论
为什么被折叠?



