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

歡迎訪(fǎng)問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

HIT 2060 Fibonacci Problem Again

發(fā)布時(shí)間:2024/1/17 编程问答 43 豆豆
生活随笔 收集整理的這篇文章主要介紹了 HIT 2060 Fibonacci Problem Again 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

點(diǎn)擊此處即可傳送HIT 2060

As we know , the Fibonacci numbers are defined as follows: F(n) == {1 n==0||n==1{F(n-1)+F(n-2) n>1; Given two numbers a and b , calculate . 從a到b之間的斐波那契數(shù)的和Input The input contains several test cases. Each test case consists of two non-negative integer numbers a and b (0 ≤ a ≤ b ≤1,000,000,000). Input is terminated by a = b = 0. Output For each test case, output S mod 1,000,000,000, since S may be quite large. Sample Input 1 1 3 5 10 1000 0 0Sample Output 1 16 496035733

題目大意:就是給你一個(gè)a和b, 求從a到b之間的斐波那契數(shù)的和

解題思路:矩陣乘法,注意考慮邊界條件:
具體詳見(jiàn)代碼:
上代碼:

/* 2015 - 8 - 14 上午 Author: ITAK 今日的我要超越昨日的我,明日的我要?jiǎng)龠^(guò)今日的我, 以創(chuàng)作出更好的代碼為目標(biāo),不斷地超越自己。 */ #include <iostream> #include <cstdio>using namespace std; const int maxn = 3; typedef long long LL; const int mod = 1e9; typedef struct {LL m[maxn][maxn]; } Matrix;Matrix P = {1,1,0,1,0,0,1,1,1,}; Matrix I = {1,0,0,0,1,0,0,0,1,}; Matrix matrix_mul(Matrix a, Matrix b) {int i, j, k;Matrix c;for(i=0; i<maxn; i++){for(j=0; j<maxn; j++){c.m[i][j] = 0;for(k=0; k<maxn; k++)c.m[i][j] += (a.m[i][k] * b.m[k][j]) % mod;c.m[i][j] %= mod;}}return c; }Matrix quick_mod(LL m) {Matrix ans = I, b = P;while(m){if(m & 1)ans = matrix_mul(ans, b);m >>= 1;b = matrix_mul(b, b);}return ans; } int main() {LL a, b;while(~scanf("%lld%lld",&a,&b)){Matrix tmp1, tmp2;if(!a && !b)break;if(a == 0){if(b == 1)puts("2");else{tmp2 = quick_mod(b-1);LL ans2 = tmp2.m[2][0]+tmp2.m[2][1]+2*tmp2.m[2][2];LL ans = (ans2%mod-1)%mod;if(ans < 0)ans += mod;printf("%lld\n",ans+1);}}else if(a == 1){if(b == 1)puts("1");else{tmp2 = quick_mod(b-1);LL ans2 = tmp2.m[2][0]+tmp2.m[2][1]+2*tmp2.m[2][2];LL ans = (ans2%mod-1)%mod;if(ans < 0)ans += mod;printf("%lld\n",ans);}}else{tmp1 = quick_mod(a-2);tmp2 = quick_mod(b-1);LL ans1 = tmp1.m[2][0]+tmp1.m[2][1]+2*tmp1.m[2][2];LL ans2 = tmp2.m[2][0]+tmp2.m[2][1]+2*tmp2.m[2][2];//cout<<ans1 << " "<<ans2<<" ";LL ans = (ans2%mod - ans1%mod);if(ans < 0)ans += mod;printf("%lld\n",ans);}}return 0; }

總結(jié)

以上是生活随笔為你收集整理的HIT 2060 Fibonacci Problem Again的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。