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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

SDNU 1062.Fibonacci(矩阵快速幂)

發布時間:2024/4/15 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 SDNU 1062.Fibonacci(矩阵快速幂) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Description

In the Fibonacci integer sequence, F0 = 0, F1 = 1, and Fn = Fn ? 1 + Fn ? 2 for n ≥ 2.

Input

a single line containing n (where 0 ≤ n ≤ 100,000,000,000)

Output

print Fn mod 1000000007 in a single line.

Sample Input

99999999999

Sample Output

669753982

Hint

An alternative formula for the Fibonacci sequence is


As a reminder, matrix multiplication is associative, and the product of two 2 × 2 matrices is given by


Also, note that raising any 2 × 2 matrix to the 0th power gives the identity matrix:

Source

Unknown 思路:一開始想著要暴力的辦法,或者用python,但還是tle了,然后發現這玩意兒可以用矩陣快速冪,就來一波騷操作了。 #include<bits/stdc++.h> using namespace std;#define ll long long #define eps 1e-9 #define pi acos(-1)const int inf = 0x3f3f3f3f; const int mod = 1000000007; const int maxn = 1000 + 8;ll n;struct matrix {ll m[2][2]; }b, tp, res, init;matrix mul(matrix a, matrix b) {matrix c;for(int i = 0; i < 2; i++){for(int j = 0; j < 2; j++){c.m[i][j] = 0;for(int k = 0; k < 2; k++){c.m[i][j] += (a.m[i][k] * b.m[k][j]) % mod;c.m[i][j] %= mod;}}}return c; }matrix matrix_mi(matrix p, ll k) {matrix t = res;while(k){if(k & 1)t = mul(t, p);k >>= 1;p = mul(p, p);}return t; }int main() {std::ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);for(int i = 0; i < 2; i++)for(int j = 0; j < 2; j++){if(i == 1 && j == 1)init.m[i][j] = 0;elseinit.m[i][j] = 1;}cin >> n;b = init;for(int i = 0; i < 2; i++)for(int j = 0; j < 2; j++)if(i == j)res.m[i][j] = 1;elseres.m[i][j] = 0;tp = matrix_mi(b, n);cout << tp.m[0][1] <<'\n';return 0; }

?

轉載于:https://www.cnblogs.com/RootVount/p/11469372.html

總結

以上是生活随笔為你收集整理的SDNU 1062.Fibonacci(矩阵快速幂)的全部內容,希望文章能夠幫你解決所遇到的問題。

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