luoguP3390 (fast power matrix template title)

Link: https: //www.luogu.org/problemnew/show/P3390

The meaning of problems: rapid power matrix template questions, ideas and fast consistent power, can only provide a matrix multiplication.

AC Code:

#include<cstdio>
#include<cstring>
using namespace std;

typedef long long LL;

const int MOD=1e9+7;
int n;
LL k;

struct Mat{
    LL m[105][105];
}a,e;

Mat mul(Mat& x,Mat& y){
    Mat res;
    memset(res.m,0,sizeof(res.m));
    for(int i=1;i<=n;++i)
        for(int j=1;j<=n;++j)
            for(int l=1;l<=n;++l){
                res.m[i][j]+=x.m[i][l]*y.m[l][j]%MOD;
                res.m[i][j]%=MOD;
            }
    return res;
}

Mat qpow(Mat& x,LL k){
    Mat ans=e;
    while(k){
        if(k&1) ans=mul(ans,x);
        x=mul(x,x);
        k>>=1;
    }
    return ans;
}

int main(){
    scanf("%d%lld",&n,&k);
    for(int i=1;i<=n;++i)
        for(int j=1;j<=n;++j)
            scanf("%lld",&a.m[i][j]);
    for(int i=1;i<=n;++i)
        e.m[i][i]=1;
    Mat ans=qpow(a,k);
    for(int i=1;i<=n;++i){
        for(int j=1;j<=n;++j)
            printf("%lld ",ans.m[i][j]);
        printf("\n");
    }
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/FrankChen831X/p/11204151.html