日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

ZOJ 3702 Gibonacci number(数学推导题)

發布時間:2025/3/16 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 ZOJ 3702 Gibonacci number(数学推导题) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

題目鏈接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3702 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?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 題意:給出一個數列的第0項和第i項的值以及i,求第j項的值是多少。其中G(0)=1,G[i] = G[i-1] + G[i-2]。 分析:經過推導可以發現,G[i] = fib[i-2] * G[0] + fib[i-1] * G[1],又因為G[0]=1,所以G[i] = fib[i-2] + fib[i-1] * G[1]其中fib[i]表示斐波那契數列的第i項。所以我們只需要求出G[1],就可以求出G[j]了。注意有些要特判。 #include <iostream> #include <cstdio> #include <cmath> #include <cstdlib> #include <vector> #include <set> #include <map> #include <stack> #include <queue> #include <cstring> #include <string> using namespace std;typedef long long LL; const int N = 1e5 + 10; long long fib[25];void Init() { // 預處理出斐波那契數列數列的前21項fib[0] = fib[1] = 1;for(int i = 2; i <= 20; i++)fib[i] = fib[i-1] + fib[i-2]; }int main() {Init();int T;LL a, n, k;scanf("%d", &T);while(T--) {scanf("%lld%lld%lld", &a, &n, &k);if(a == 1) { // 若G[1]已給出if(k == 1) printf("%lld\n", n);else {printf("%lld\n", fib[k-2] + fib[k-1] * n);}}else {// G[i] = fib[i-2] + fib[i-1] * G[1]LL p = n - fib[a-2];if(p % fib[a-1] != 0 || p <= 0) {printf("-1\n");continue;}else {LL q = p / fib[a-1];if(k == 1) printf("%lld\n", q);else printf("%lld\n", fib[k-2] + fib[k-1] * q);}}}return 0; }


總結

以上是生活随笔為你收集整理的ZOJ 3702 Gibonacci number(数学推导题)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。