C++-蓝桥杯-矩阵乘法[快速幂]

忘了改矩阵的大小居然还有33分,我醉了

 1 #include <cstdio>
 2 const int N=35;
 3 struct Matrix{int a[N][N];};
 4 int n,m;
 5 Matrix A,O,I;
 6 Matrix Mul(Matrix A,Matrix B){
 7     Matrix C=O;
 8     for(int i=1;i<=n;i++)
 9         for(int j=1;j<=n;j++)
10             for(int k=1;k<=n;k++)
11                 C.a[i][j]+=A.a[i][k]*B.a[k][j];
12     return C;
13 }
14 Matrix Pow(Matrix A,int n){
15     Matrix B=I;
16     for(;n;n>>=1,A=Mul(A,A))if(n&1)B=Mul(B,A);
17     return B;
18 }
19 int main(){
20     for(int i=0;i<N;i++)for(int j=0;j<N;j++)O.a[i][j]=0,I.a[i][j]=(i==j);
21     scanf("%d%d",&n,&m);
22     for(int i=1;i<=n;i++)for(int j=1;j<=n;j++)scanf("%d",&A.a[i][j]);A=Pow(A,m);
23     for(int i=1;i<=n;i++){for(int j=1;j<=n;j++)printf("%d ",A.a[i][j]);puts("");}
24     return 0;
25 }

 

posted @ 2020-02-27 22:16  Jason-Cow  阅读(...)  评论(...编辑  收藏