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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

poj 3233 Matrix Power Series

發布時間:2023/12/4 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 poj 3233 Matrix Power Series 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Matrix Power Series

思路

題意比較簡單,就是要求S(n)=∑i=1nAiS(n) = \sum _{i = 1} ^{n} A^ {i}S(n)=i=1n?Ai,顯然有S(n)=S(n?1)?A+AS(n) = S(n - 1) * A + AS(n)=S(n?1)?A+A,看到這里,那就簡單了,遞推式,加矩陣,矩陣快速冪無疑了嘛,所以我們開始構造矩陣。

顯然有如下矩陣,EEE是單位矩陣,AAA是輸入矩陣,OOO是零矩陣。

[EEOA]?[OOAO]\begin{bmatrix} E & E \\ O & A\end{bmatrix} * \begin{bmatrix} O & O\\ A & O \end{bmatrix}[EO?EA?]?[OA?OO?]

通過這個矩陣的遞推,我們就可以通過快速冪,達到快速求解的目的。

我嚴重懷疑這道題目數據有問題,longlonglong\ longlong?longwawawa,然后intintint就過了???

AC代碼

/*Author : lifehappy */ // #pragma GCC optimize(2) // #pragma GCC optimize(3) // #include <bits/stdc++.h>#include <cstdio> #include <iostream> #include <stdlib.h> #include <algorithm> #include <cmath>#define mp make_pair #define pb push_back #define endl '\n'using namespace std;typedef long long ll; typedef unsigned long long ull; typedef pair<int, int> pii;const double pi = acos(-1.0); const double eps = 1e-7; const int inf = 0x3f3f3f3f;inline ll read() {ll f = 1, x = 0;char c = getchar();while(c < '0' || c > '9') {if(c == '-') f = -1;c = getchar();}while(c >= '0' && c <= '9') {x = (x << 1) + (x << 3) + (c ^ 48);c = getchar();}return f * x; }void print(ll x) {if(x < 10) {putchar(x + 48);return ;}print(x / 10);putchar(x % 10 + 48); }const int N = 70;int n, k, mod;struct matrix {int a[N][N];matrix operator * (const matrix & t) const {matrix temp;for(int i = 1; i <= 2 * n; i++) {for(int j = 1; j <= 2 * n; j++) {temp.a[i][j] = 0;for(int k = 1; k <= 2 * n; k++) {temp.a[i][j] = (temp.a[i][j] + a[i][k] * t.a[k][j]) % mod;}}}return temp;} }E, A, O;int main() {// freopen("in.txt", "r", stdin);// freopen("out.txt", "w", stdout);// ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);n = read(), k = read(), mod = read();matrix fat, ans;for(int i = 1; i <= 2 * n; i++) {//先置零,for(int j = 1; j <= 2 * n; j++) {fat.a[i][j] = ans.a[i][j] = 0;}}for(int i = 1; i <= n; i++) {//讀入的時候置A加上置E矩陣。for(int j = 1; j <= n; j++) {fat.a[i + n][j + n] = ans.a[i + n][j] = read();}fat.a[i][i] = fat.a[i][i + n] = 1;}while(k) {if(k & 1) ans = fat * ans;fat = fat * fat;k >>= 1;}for(int i = 1; i <= n; i++) {for(int j = 1; j <= n; j++) {printf("%d%c", ans.a[i][j], j == n ? '\n' : ' ');}}return 0; }

調不出來的代碼

寫了一手逼格高一點的舉證套矩陣的重載操作符的寫法,可是太菜了,調不出來

/*Author : lifehappy */ // #pragma GCC optimize(2) // #pragma GCC optimize(3) // #include <bits/stdc++.h>#include <cstdio> #include <iostream> #include <stdlib.h> #include <algorithm> #include <cmath>#define mp make_pair #define pb push_back #define endl '\n'using namespace std;typedef long long ll; typedef unsigned long long ull; typedef pair<int, int> pii;const double pi = acos(-1.0); const double eps = 1e-7; const int inf = 0x3f3f3f3f;inline ll read() {ll f = 1, x = 0;char c = getchar();while(c < '0' || c > '9') {if(c == '-') f = -1;c = getchar();}while(c >= '0' && c <= '9') {x = (x << 1) + (x << 3) + (c ^ 48);c = getchar();}return f * x; }void print(ll x) {if(x < 10) {putchar(x + 48);return ;}print(x / 10);putchar(x % 10 + 48); }const int N = 70;int n, k, mod;struct matrix {int a[N][N];matrix operator * (const matrix & t) const {matrix temp;for(int i = 1; i <= 2 * n; i++) {for(int j = 1; j <= 2 * n; j++) {temp.a[i][j] = 0;for(int k = 1; k <= 2 * n; k++) {temp.a[i][j] = (temp.a[i][j] + a[i][k] * t.a[k][j]) % mod;}}}return temp;}matrix operator + (const matrix & t) const {matrix temp;for(int i = 1; i <= n; i++) {for(int j = 1; j <= n; j++) {temp.a[i][j] = (a[i][j] + t.a[i][j]) % mod;}}return temp;} }E, A, O;struct Matrix {matrix a[3][3];Matrix operator * (const Matrix & t) const {Matrix temp;for(int i = 1; i <= 2; i++) {for(int j = 1; j <= 2; j++) {temp.a[i][j] = O;for(int k = 1; k <= 2; k++) {temp.a[i][j] = (a[i][k] * t.a[k][j]) + temp.a[i][j];}}}} };int main() {// freopen("in.txt", "r", stdin);// freopen("out.txt", "w", stdout);// ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);n = read(), k = read(), mod = read();for(int i = 1; i <= n; i++) {for(int j = 1; j <= n; j++) {A.a[i][j] = read();E.a[i][j] = O.a[i][j] = 0;}E.a[i][i] = 1;}Matrix fat, ans;fat.a[1][1] = E, fat.a[1][2] = E, fat.a[2][1] = O, fat.a[2][2] = A;ans.a[1][1] = O, ans.a[1][2] = O, ans.a[2][1] = A, ans.a[2][2] = O;while(k) {if(k & 1) ans = ans * fat;fat = fat * fat;k >>= 1;}for(int i = 1; i <= n; i++) {for(int j = 1; j <= n; j++) {printf("%d%c", ans.a[1][1].a[i][j], j == n ? '\n' : ' ');}}return 0; }

總結

以上是生活随笔為你收集整理的poj 3233 Matrix Power Series的全部內容,希望文章能夠幫你解決所遇到的問題。

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