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

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

生活随笔

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

编程问答

M斐波那契数列(HDU-4549)

發(fā)布時(shí)間:2025/3/17 编程问答 20 豆豆
生活随笔 收集整理的這篇文章主要介紹了 M斐波那契数列(HDU-4549) 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

Problem Description

M斐波那契數(shù)列F[n]是一種整數(shù)數(shù)列,它的定義如下:?

F[0] = a?
F[1] = b?
F[n] = F[n-1] * F[n-2] ( n > 1 )?

現(xiàn)在給出a, b, n,你能求出F[n]的值嗎?

Input

輸入包含多組測(cè)試數(shù)據(jù);?
每組數(shù)據(jù)占一行,包含3個(gè)整數(shù)a, b, n( 0 <= a, b, n <= 10^9 )

Output

對(duì)每組測(cè)試數(shù)據(jù)請(qǐng)輸出一個(gè)整數(shù)F[n],由于F[n]可能很大,你只需輸出F[n]對(duì)1000000007取模后的值即可,每組數(shù)據(jù)輸出一行。

Sample Input

0 1 0
6 10 2

Sample Output

0
60

思路:

根據(jù) F[0]=a,F[1]=b 與?F[n] = F[n-1] * F[n-2] 進(jìn)行遞推

有:

其中,f[i] 為斐波那契數(shù)列數(shù)列,即:f[i]=f[i-1]+f[i-2]

因此,可先根據(jù)斐波那契數(shù)列構(gòu)造矩陣,求出 f[n-1] 與 f[n],然后直接求 F[n]

構(gòu)造矩陣,有:

化簡(jiǎn),得:

那么:

由于 n 最大可達(dá)到 1E9,f[n] 與 f[n-1] 會(huì)很大,要求的 ?一定會(huì)爆

因此要使用費(fèi)馬小定理降冪

根據(jù)費(fèi)馬小定理:

那么:

Source Program

#include<iostream> #include<cstdio> #include<cstdlib> #include<string> #include<cstring> #include<cmath> #include<ctime> #include<algorithm> #include<utility> #include<stack> #include<queue> #include<vector> #include<set> #include<map> #define PI acos(-1.0) #define E 1e-9 #define INF 0x3f3f3f3f #define LL long long const int MOD=1000000007; const int N=10+5; const int dx[]= {-1,1,0,0}; const int dy[]= {0,0,-1,1}; using namespace std; struct Matrix{LL s[N][N]; }; Matrix e;//單位矩陣E Matrix x;//構(gòu)造矩陣void init(){for(int i=1;i<=2;i++)//主對(duì)角線為1e.s[i][i]=1; } Matrix mul(Matrix A,Matrix B,LL n){//矩陣乘法,n代表A、B兩個(gè)矩陣是n階方陣Matrix temp;//臨時(shí)矩陣,存放A*B結(jié)果for(int i=1;i<=n;i++)for(int j=1;j<=n;j++)temp.s[i][j]=0;for(int i=1;i<=n;i++)for(int j=1;j<=n;j++)for(int k=1;k<=n;k++)temp.s[i][j]=(temp.s[i][j]+A.s[i][k]*B.s[k][j])%(MOD-1);//費(fèi)馬小定理降冪后模為1E9-1return temp; } Matrix quickPower(Matrix a,LL b,LL n){//矩陣快速冪,求矩陣n階矩陣的b次冪Matrix ans=e;while(b){if(b&1)ans=mul(ans,a,n);//ans=e*aa=mul(a,a,n);//a=a*ab>>=1;}return ans; } LL quickPow(LL a,LL b){//快速冪LL res=1;while(b){if(b&1)res=(res*a)%MOD;a=(a*a)%MOD;b>>=1;}return res; } int main(){init();LL a,b,n;while(scanf("%lld%lld%lld",&a,&b,&n)!=EOF){LL fa;//a的冪LL fb;//b的冪if(n==0){fa=1;fb=0;}else if(n==1){fa=0;fb=1;}else if(n==2){fa=1;fb=1;}else{x.s[1][1]=1;x.s[1][2]=1;x.s[2][1]=1;x.s[2][2]=0;Matrix res=quickPower(x,n-2,2);//注意MOD降冪fa=((res.s[2][1]+res.s[2][2])%(MOD-1)+(MOD-1))%(MOD-1);//fn-1fb=((res.s[1][1]+res.s[1][2])%(MOD-1)+(MOD-1))%(MOD-1);//fn}LL temp1=quickPow(a,fa);//a^f(n-1)LL temp2=quickPow(b,fb);//b^f(n)LL result=(temp1*temp2)%MOD;printf("%lld\n",result);}return 0; }

?

新人創(chuàng)作打卡挑戰(zhàn)賽發(fā)博客就能抽獎(jiǎng)!定制產(chǎn)品紅包拿不停!

總結(jié)

以上是生活随笔為你收集整理的M斐波那契数列(HDU-4549)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

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