Recursive sequence HDU - 5950
生活随笔
收集整理的這篇文章主要介紹了
Recursive sequence HDU - 5950
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Recursive sequence HDU - 5950
題意:
給你一個式子:f[n]=2f[n-2]+f[n-1]+n4
給你f[1]和f[2],給你一個n,求f[n]
f[1],f[2],n<=231
題解:
很明顯,矩陣快速冪,但是太久沒做這種題,我都忘了怎么推導矩陣的了
代碼:
#include <iostream> #include <cstdio> #include <cstring> #include <queue>using namespace std;typedef long long ll; int t, n, a, b; ll mod = 2147493647; struct Matrix {ll mat[15][15];Matrix(){memset(mat, 0, sizeof(mat));}friend Matrix operator * (Matrix A, Matrix B){Matrix ans;for(int i = 1; i <= 7; ++ i){for(int j = 1; j <= 7; ++ j){for(int k = 1; k <= 7; ++ k){ans.mat[i][j] += (A.mat[i][k] * B.mat[k][j]) % mod;ans.mat[i][j] %= mod;}}}return ans;} };Matrix quick_matrix(Matrix A, int b) {Matrix ans;for(int i = 1; i <= 7; ++ i){ans.mat[i][i] = 1;}while(b){if(b & 1){ans = ans * A;}A = A * A;b >>= 1;}return ans; }int main() {cin >> t;while(t--){cin >> n >> a >> b;if(n == 1 || n == 2){if(n == 1)cout << a << endl;else cout << b << endl;continue;}Matrix A, B;A.mat[1][1] = b;A.mat[2][1] = a;A.mat[3][1] = 3 * 3 * 3 * 3;A.mat[4][1] = 3 * 3 * 3;A.mat[5][1] = 3 * 3;A.mat[6][1] = 3;A.mat[7][1] = 1;B.mat[1][1] = 1;B.mat[1][2] = 2;B.mat[1][3] = 1;B.mat[2][1] = 1;B.mat[3][3] = 1;B.mat[3][4] = 4;B.mat[3][5] = 6;B.mat[3][6] = 4;B.mat[3][7] = 1;B.mat[4][4] = 1;B.mat[4][5] = 3;B.mat[4][6] = 3;B.mat[4][7] = 1;B.mat[5][5] = 1;B.mat[5][6] = 2;B.mat[5][7] = 1;B.mat[6][6] = 1;B.mat[6][7] = 1;B.mat[7][7] = 1;B = quick_matrix(B, n - 2);A = B * A;cout << A.mat[1][1] << endl;}return 0; }總結
以上是生活随笔為你收集整理的Recursive sequence HDU - 5950的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 臭草叶子的功效与作用、禁忌和食用方法
- 下一篇: 山药叶子的功效与作用、禁忌和食用方法