矩阵求幂
Posted maoruimas
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了矩阵求幂相关的知识,希望对你有一定的参考价值。
1 #include<bits/stdc++.h> 2 using namespace std; 3 #define rep(i,a,b) for(int i=a;i<=b;++i) 4 #define ms(arr,a) memset(arr,a,sizeof arr) 5 #define debug(x) cout<<"< "#x" = "<<x<<" >"<<endl 6 #define sd(x) scanf("%d",&x) 7 #define slf(x) scanf("%lf",&x) 8 #define pn printf(" ") 9 const int maxn=1e2+5; 10 struct Matrix 11 { 12 int n; 13 double rec[maxn][maxn]; 14 Matrix(){n=0;ms(rec,0);} 15 Matrix(int x){n=x;ms(rec,0);} 16 Matrix(int x,int k) 17 { 18 n=x; 19 ms(rec,0); 20 rep(i,1,n)rec[i][i]=k; 21 } 22 Matrix operator *(const Matrix b)const 23 { 24 Matrix ret(n); 25 rep(i,1,n) 26 rep(j,1,n) 27 rep(k,1,n) 28 ret.rec[i][j]+=rec[i][k]*b.rec[k][j]; 29 return ret; 30 } 31 }; 32 Matrix Pow(Matrix a,int n) 33 { 34 Matrix ret(a.n,1); 35 while(n) 36 { 37 if(n&1)ret=ret*a; 38 a=a*a; 39 n>>=1; 40 } 41 return ret; 42 } 43 int main() 44 { 45 freopen("Input.txt","r",stdin); 46 //freopen("Output.txt","w",stdout); 47 //printf("Matrix size:"); 48 Matrix a; 49 sd(a.n); 50 rep(i,1,a.n) 51 rep(j,1,a.n) 52 slf(a.rec[i][j]); 53 //printf("Pow:"); 54 int N; 55 while(~sd(N)) 56 { 57 a=Pow(a,N); 58 rep(i,1,a.n) 59 { 60 rep(j,1,a.n) 61 printf("%lf ",a.rec[i][j]); 62 pn; 63 } 64 pn; 65 } 66 //freopen("CON","w",stdout); 67 //system("start Output.txt"); 68 }
以上是关于矩阵求幂的主要内容,如果未能解决你的问题,请参考以下文章