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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

HDU4549 M斐波那契数列 —— 斐波那契、费马小定理、矩阵快速幂

發(fā)布時間:2025/4/14 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 HDU4549 M斐波那契数列 —— 斐波那契、费马小定理、矩阵快速幂 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

題目鏈接:https://vjudge.net/problem/HDU-4549

?

M斐波那契數(shù)列

Time Limit: 3000/1000 MS (Java/Others)????Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 4492????Accepted Submission(s): 1397


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 輸入包含多組測試數(shù)據(jù);
每組數(shù)據(jù)占一行,包含3個整數(shù)a, b, n( 0 <= a, b, n <= 10^9 )

?

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

?

Sample Input 0 1 0 6 10 2

?

Sample Output 0 60

?

Source 2013金山西山居創(chuàng)意游戲程序挑戰(zhàn)賽——初賽(2)

?

Recommend liuyiding

?

?

題解:

1.可知f[n]中a、b的指數(shù)符合斐波那契數(shù)列,因而可用矩陣快速冪求出。

2.然而如果指數(shù)不求模,也可能會溢出。但指數(shù)又怎樣求模呢?

有如下定理:當(dāng)m為素?cái)?shù),且a、n互質(zhì)時, a^n % m = a^(n%(m-1)) % m

證明:

根據(jù)費(fèi)馬小定理,當(dāng)m為素?cái)?shù),且a、p互質(zhì)時, a^(m-1)?≡ 1 (mod m),

a^n = a^(k*(m-1)+r) = (a^(m-1))^k * a^r,其中 r = n%(m-1),

那么 a^n % m = ( (a^(m-1))^k * a^r ) % m =??(a^(m-1))^k % m *?a^r % m = 1 *?a^r % m =?a^(n%(m-1)) % m 。

所以:當(dāng)m為素?cái)?shù),且a、n互質(zhì)時, a^n % m = a^(n%(m-1)) % m

?

?

代碼如下:

1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 #include <vector> 6 #include <cmath> 7 #include <queue> 8 #include <stack> 9 #include <map> 10 #include <string> 11 #include <set> 12 using namespace std; 13 typedef long long LL; 14 const int INF = 2e9; 15 const LL LNF = 9e18; 16 const int MOD = 1000000007; 17 const int MAXN = 1e6+100; 18 19 const int Size = 2; 20 struct MA 21 { 22 LL mat[Size][Size]; 23 void init() 24 { 25 for(int i = 0; i<Size; i++) 26 for(int j = 0; j<Size; j++) 27 mat[i][j] = (i==j); 28 } 29 }; 30 31 MA mul(MA x, MA y) 32 { 33 MA ret; 34 memset(ret.mat, 0, sizeof(ret.mat)); 35 for(int i = 0; i<Size; i++) 36 for(int j = 0; j<Size; j++) 37 for(int k = 0; k<Size; k++) 38 ret.mat[i][j] += (1LL*x.mat[i][k]*y.mat[k][j])%(MOD-1), ret.mat[i][j] %= (MOD-1); 39 return ret; 40 } 41 42 MA qpow(MA x, LL y) 43 { 44 MA s; 45 s.init(); 46 while(y) 47 { 48 if(y&1) s = mul(s, x); 49 x = mul(x, x); 50 y >>= 1; 51 } 52 return s; 53 } 54 55 LL q_pow(LL x, LL y) 56 { 57 LL s = 1; 58 while(y) 59 { 60 if(y&1) s *= x, s %= MOD; 61 x *= x, x %= MOD; 62 y >>= 1; 63 } 64 return s; 65 } 66 67 MA tmp = { 68 1, 1, 69 1, 0 70 }; 71 72 int main() 73 { 74 LL f[2], n; 75 while(scanf("%lld%lld%lld",&f[0],&f[1],&n)!=EOF) 76 { 77 if(n<=1) 78 { 79 printf("%lld\n", f[n]%MOD); 80 continue; 81 } 82 83 MA s = tmp; 84 s = qpow(s, n-1); 85 LL p1 = s.mat[0][0]; 86 LL p2 = s.mat[1][0]; 87 LL ans = q_pow(f[0], p2)*q_pow(f[1], p1)%MOD; 88 printf("%lld\n", ans); 89 } 90 } View Code

?

轉(zhuǎn)載于:https://www.cnblogs.com/DOLFAMINGO/p/8417182.html

總結(jié)

以上是生活随笔為你收集整理的HDU4549 M斐波那契数列 —— 斐波那契、费马小定理、矩阵快速幂的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

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