Given n*n matrix A, ask A^k
The ranks are all n
#include <iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>using namespacestd;Const intn= the, mod=1000000007; typedefLong Longll;inline ll Read () {CharC=getchar (); ll x=0, f=1; while(c<'0'|| C>'9'){if(c=='-') f=-1; c=GetChar ();} while(c>='0'&&c<='9') {x=x*Ten+c-'0'; c=GetChar ();} returnx*F;} ll N,k;structmat{ll Mt[n][n]; Mat () {memset (MT,0,sizeof(MT));}} A,im,ans;voidinit () { for(intI=1; i<=n;i++) im.mt[i][i]=1;} Mat Mul (Mat&a,mat &b) {Mat C; for(intI=1; i<=n;i++) for(intk=1; k<=n;k++)if(A.mt[i][k]) for(intj=1; j<=n;j++) C.mt[i][j]= (C.mt[i][j]+a.mt[i][k]*b.mt[k][j])%MOD; returnC;}voidPow (Mat &A,ll B) {ans=im; for(; b;b>>=1, a=Mul (a,a))if(b&1) ans=Mul (ans,a);}intMain () {n=read (); k=read (); for(intI=1; i<=n;i++) for(intj=1; j<=n;j++) a.mt[i][j]=read (); Init (); Pow (a,k); for(intI=1; i<=n;i++){ for(intj=1; j<=n;j++) printf ("%lld", Ans.mt[i][j]); Putchar ('\ n'); }}
luoguP3390 "template" Matrix fast power