BASIC-17 VIP试题 矩阵乘法

  • 可用矩阵快速幂小试牛刀
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int,int> P;
const int N=31;
const int M=2e5+9;
#define gt getchar();
#define line '\n'
#define is isdigit
int read(){int x=0,op=1;char c=gt;while(!is(c)){if(c=='-')op=-1;c=gt;}while(is(c))x=x*10+c-48,c=gt;return x*op;}
struct mat{
	int A[N][N];
	mat friend operator*(mat X,mat Y){
		mat ans;memset(ans.A,0,sizeof(ans.A));
		for(int i=0;i<N;++i)
			for(int j=0;j<N;++j)
				for(int k=0;k<N;++k)
					ans.A[i][j]+=X.A[i][k]*Y.A[k][j];
		return ans;
	}
};
mat mat_qpow(mat x,int n)
{
	mat ans;
	for(int i=0;i<N;++i)for(int j=0;j<N;++j)if(i==j)ans.A[i][j]=1;else ans.A[i][j]=0;
	while(n){
		if(n&1)ans=ans*x;
		n>>=1;
		x=x*x;
	}
	return ans;
}
int main()
{
	int n=read(),m=read();
	mat x;memset(x.A,0,sizeof(x.A));
	for(int i=0;i<n;++i)for(int j=0;j<n;++j)x.A[i][j]=read();
	mat ans=mat_qpow(x,m);
	for(int i=0;i<n;++i){
		cout<<ans.A[i][0];
		for(int j=1;j<n;++j)
			cout<<" "<<ans.A[i][j];
		cout<<line;
	}
	return 0;
}
posted @ 2020-04-15 21:42  啥也不是*  阅读(132)  评论(0)    收藏  举报