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

题目的意思是给出Gibonacci某一项的值,求出给定项

设G(1)=x;

则G(0)=1,G(1)=x,G(2)=1+x,G(3)=2x+1,G(4)=3x+2;

我们发现x的系数和常数符合斐波那契数列,推导公式即可

注意G(1)=0的时候输出-1;

#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;
}

ZOJ 3702 Gibonacci number 2017-04-06 23:28 28人阅读 评论(0) 收藏的更多相关文章

  1. Number of Containers(数学) 分类: 数学 2015-07-07 23:42 1人阅读 评论(0) 收藏

    Number of Containers Time Limit: 1 Second Memory Limit: 32768 KB For two integers m and k, k is said ...

  2. ZOJ2482 IP Address 2017-04-18 23:11 44人阅读 评论(0) 收藏

    IP Address Time Limit: 2 Seconds      Memory Limit: 65536 KB Suppose you are reading byte streams fr ...

  3. 动态链接库(DLL) 分类: c/c++ 2015-01-04 23:30 423人阅读 评论(0) 收藏

    动态链接库:我们经常把常用的代码制作成一个可执行模块供其他可执行文件调用,这样的模块称为链接库,分为动态链接库和静态链接库. 对于静态链接库,LIB包含具体实现代码且会被包含进EXE中,导致文件过大, ...

  4. NYOJ-235 zb的生日 AC 分类: NYOJ 2013-12-30 23:10 183人阅读 评论(0) 收藏

    DFS算法: #include<stdio.h> #include<math.h> void find(int k,int w); int num[23]={0}; int m ...

  5. HDU 2034 人见人爱A-B 分类: ACM 2015-06-23 23:42 9人阅读 评论(0) 收藏

    人见人爱A-B Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Su ...

  6. HDU 2035 人见人爱A^B 分类: ACM 2015-06-22 23:54 9人阅读 评论(0) 收藏

    人见人爱A^B Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Su ...

  7. HDU2033 人见人爱A+B 分类: ACM 2015-06-21 23:05 13人阅读 评论(0) 收藏

    人见人爱A+B Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Su ...

  8. SCU 4440 分类: ACM 2015-06-20 23:58 16人阅读 评论(0) 收藏

    SCU - 4440 Rectangle Time Limit: Unknown   Memory Limit: Unknown   64bit IO Format: %lld & %llu ...

  9. HDU2680 Choose the best route 最短路 分类: ACM 2015-03-18 23:30 37人阅读 评论(0) 收藏

    Choose the best route Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot ...

  10. Hdu 1009 FatMouse' Trade 2016-05-05 23:02 86人阅读 评论(0) 收藏

    FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Tot ...

随机推荐

  1. Python项目输出小类概率,机器学习

    from pandas import read_csv import numpy as np from sklearn.datasets.base import Bunch import pickle ...

  2. Python 常用 PEP8 编码规范和建议

    代码布局 缩进 每级缩进用4个空格. 括号中使用垂直隐式缩进或使用悬挂缩进. EXAMPLE: # (垂直隐式缩进)对准左括号 foo = long_function_name(var_one, va ...

  3. RPM包下载网址

    https://pkgs.org/ (最爱) http://rpm.pbone.net/ http://rpmfind.net/linux/RPM/index.html

  4. Solr -- 查询语法/参数

    1. 常用查询参数 参数 描述 defType 指定用于处理查询语句(参数q的内容)的查询解析器,eg:defType=lucene sort 指定响应的排序方式:升序asc或降序desc.同时需要指 ...

  5. 基于Spring Boot和Spring Cloud实现微服务架构学习

    转载自:http://blog.csdn.net/enweitech/article/details/52582918 看了几周Spring相关框架的书籍和官方demo,是时候开始总结下这中间的学习感 ...

  6. centOs 安装NPM 记录

    1. 关闭npm严格认证 npm config set strict-ssl false

  7. 【独家】阿里天池IJCAI17大赛第四名方案全解析(附代码)

    [独家]阿里天池IJCAI17大赛第四名方案全解析(附代码) https://mp.weixin.qq.com/s?__biz=MzAxMzA2MDYxMw==&mid=2651560625& ...

  8. 信息传递(tarjan)

    信息传递 http://uoj.ac/problem/146 有 n 个同学(编号为 1 到n )正在玩一个信息传递的游戏.在游戏里每人都有一个固定的信息传递对象,其中,编号为 i的同学的信息传递对象 ...

  9. 获取set()和push()方法向值栈放的数据

    ------------------siwuxie095 获取 set() 方法向值栈放的数据 1.具体步骤 (1)在 Action 中使用 set() 方法向值栈放数据 (2)在 JSP 页面中从值 ...

  10. 微软AntiXSS防止xss攻击类库

    AntiXSS,由微软推出的用于防止XSS攻击的一个类库,可实现输入白名单机制和输出转义. AntiXSS最新版的下载地址:http://wpl.codeplex.com 下载安装之后,安装目录下有以 ...