ZOJ 3702 Gibonacci number 找规律
来源:互联网 发布:燕十八mysql优化 编辑:程序博客网 时间:2024/05/22 13:37
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 forG(1) is a random integert, (t>=1). Given thei-th Gibonacci number valueG(i), and the numberj, your task is to output the value forG(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 contains3 integersi, 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 fort, output -1.
Sample Input
41 1 23 5 43 4 612 17801 19
Sample Output
28-1516847
题目是说定义一个类似于Fibonacci数列的数列Gibonacci,这个数列的计算方式和Fibonacci一样,不同的是G(0)=1,而G(1)是一个未知数t,题目则是告诉你G(i)的值,然后计算G(j)的值,其实这个题并不复杂,列几个数就会发现,G(0)=1,G(1)=t,G(2)=1+t,G(3)=1+2t,G(4)=2+3t,G(5)=3+5t......前面的系数仍然符合Fibonacci数列的规律,而这个题目的范围只到20就结束了,所以简单打个表把Fibonacci数列前面的值都算出来,再通过规律计算t的值,就能很容易得出结果了。
下面AC代码:
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>using namespace std;int f[105];int fibonacci(){ int i; memset(f,0,sizeof(f)); f[0]=0; f[1]=1; for(i=2;i<30;i++) { f[i]=f[i-1]+f[i-2]; } return 0;}int main(){ int T; int i,j; long long t; long long ans; int g; fibonacci(); scanf("%d",&T); while(T--) { scanf("%d%d%d",&i,&g,&j); t=(g-f[i-1])%f[i]; if(t==0) t=(g-f[i-1])/f[i]; else { cout<<-1<<endl; continue; } if(t<1) { cout<<-1<<endl; continue; } ans=f[j-1]+f[j]*t; cout<<ans<<endl; } return 0;}
- ZOJ 3702 Gibonacci number 找规律
- ZOJ 3702 Gibonacci number
- zoj 3702 Gibonacci number
- ZOJ 3702 Gibonacci number
- zoj 3702 Gibonacci number
- ZOJ 3702 Gibonacci number
- ZOJ 3702 Gibonacci number
- ZOJ 3702 Gibonacci number
- ZOJ 3702 Gibonacci number【思维】
- ZOJ3702 Gibonacci number (找规律/不看题解好了)
- ZOJ Problem Set - 3702 Gibonacci number
- ZOJ-#3622 Magic Number(找规律)
- zoj 3622 Magic Number(找规律)
- ZOJ 3702 Gibonacci number(数学推导题)
- zoj 3622 Magic Number (找规律+打表)
- Gibonacci number
- Gibonacci number
- Gibonacci number
- HTML给标签的点击事件绑定匿名函数
- 三、zedboard Linux内核编译
- Linux上网络编程
- css3实现的简单开关按钮代码实例:
- Hibernate复合主键的注解
- ZOJ 3702 Gibonacci number 找规律
- 如何实现每天固定时间提醒设定信息
- 阿里开源库VLayout的使用笔记
- shell编程3_警系统
- SpringCloudNetflix-Feign/Hystrix Setting
- jquery 隐式迭代
- 最新Spark编程指南Python版[Spark 1.3.0][译]
- jsp 页面内获取请求地址
- NYOJ 201 作业题