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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

Recursive sequence HDU - 5950

發布時間:2023/12/3 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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的全部內容,希望文章能夠幫你解決所遇到的問題。

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