poj 3233 Matrix Power Series
生活随笔
收集整理的這篇文章主要介紹了
poj 3233 Matrix Power Series
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
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,看到這里,那就簡單了,遞推式,加矩陣,矩陣快速冪無疑了嘛,所以我們開始構(gòu)造矩陣。
顯然有如下矩陣,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?]
通過這個矩陣的遞推,我們就可以通過快速冪,達到快速求解的目的。
我嚴重懷疑這道題目數(shù)據(jù)有問題,longlonglong\ longlong?long就wawawa,然后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; }調(diào)不出來的代碼
寫了一手逼格高一點的舉證套矩陣的重載操作符的寫法,可是太菜了,調(diào)不出來
/*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; }總結(jié)
以上是生活随笔為你收集整理的poj 3233 Matrix Power Series的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 想用电脑玩游戏游戏用电脑玩游戏怎么玩
- 下一篇: 电脑监控解决方案电脑监控如何