P3390 矩阵快速幂 模板

P3390 【模板】矩阵快速幂

只是模板而已。

但是没注意到输入都要开long long,还是WA+T了几次。以后还是在不MLE的情况下全开long long,省的乱七八糟的事。

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int N=105;
const int Mod=1000000007;
int n;
long long t;
struct martix {
	long long fac[N][N];
	martix() {
		memset(fac,0,sizeof(fac));
	}
	void build() {
		for (int i=1; i<=n; i++)
			fac[i][i]=1;
	}
} Ans,Base;
martix operator *(const martix &A,const martix &B) {
	martix temp;
	for (int k=1; k<=n; k++)
		for (int i=1; i<=n; i++)
			for (int j=1; j<=n; j++)
				temp.fac[i][j]=(temp.fac[i][j]+A.fac[i][k]*B.fac[k][j]%Mod)%Mod;
	return temp;
}
int main() {
	ios::sync_with_stdio(false);
	cin>>n>>t;
	Ans.build();
	for (int i=1; i<=n; i++)
		for (int j=1; j<=n; j++)
			cin>>Base.fac[i][j];
	while (t>0) {
		if (t&1)
			Ans=Ans*Base;
		Base=Base*Base;
		t>>=1;
	}
	for (int i=1; i<=n; i++) {
		for (int j=1; j<=n; j++)
			cout<<Ans.fac[i][j]<<" ";
		cout<<endl;
	}
}

posted @ 2021-11-11 17:44  Kinuhata  阅读(26)  评论(0)    收藏  举报