SDNU 1085.爬楼梯再加强版(矩阵快速幂)
生活随笔
收集整理的這篇文章主要介紹了
SDNU 1085.爬楼梯再加强版(矩阵快速幂)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Description
WZ是個蛋痛的人,總是喜歡琢磨蛋痛的事,比如他最近想知道上樓梯總共有多少種方式。已知他一步可以邁一階、兩階或者三階,現在給你樓梯的階數,讓你計算總共有多少種方式。Input
輸入有多組數據,每組數據占一行,表示樓梯的階數。(1<=N<=100,000,000,000)Output
對于每組數據,輸出一行,表示上樓方式的總數 % 1000000007。Sample Input
1 2Sample Output
1 2Source
Unknown #include<bits/stdc++.h> using namespace std; #define ll long longconst int inf = 0x3f3f3f3f; const int mod = 1000000007; const int maxn = 1000 + 8;ll n;struct matrix {ll m[3][3]; }b, tp, res, init;matrix mul(matrix a, matrix b) {matrix c;for(int i = 0; i < 3; i++){for(int j = 0; j < 3; j++){c.m[i][j] = 0;for(int k = 0; k < 3; 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 < 3; i++)for(int j = 0; j < 3; j++)init.m[i][j] = 0;for(int i = 0; i < 3; i++)init.m[0][i] = 1;init.m[1][0] = 1;init.m[2][1] = 1;for(int i = 0; i < 3; i++)for(int j = 0; j < 3; j++)if(i == j)res.m[i][j] = 1;elseres.m[i][j] = 0;while(cin >> n){b = init;if(n == 1)cout << "1" << '\n';else if(n == 2)cout << "2" << '\n';else if(n == 3)cout << "4" << '\n';else{tp = matrix_mi(b, n - 3);cout << ((4 * tp.m[0][0]) % mod + (2 * tp.m[0][1]) % mod + tp.m[0][2] % mod) % mod << '\n';}}return 0; }?
轉載于:https://www.cnblogs.com/RootVount/p/11559755.html
總結
以上是生活随笔為你收集整理的SDNU 1085.爬楼梯再加强版(矩阵快速幂)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: pandas之loc iloc ix
- 下一篇: SDNU 1430.十六进制转八进制(p