luogu_3390 【模板】矩阵快速幂

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
typedef long long LL;
const LL Mod=1e9+7;
int n;
LL k;
struct Matrix{
    static const int N=101;
    int n;
    LL h[N][N];
    void clear(int n){
        this->n=n;
        memset(h,0,sizeof(h));
    }
    friend ostream& operator << (ostream&,Matrix&);
    friend istream& operator >> (istream&,Matrix&);
    Matrix operator * (const Matrix &t) const{
        Matrix b;b.clear(n);
        for(int i=1;i<=n;i++)
            for(int j=1;j<=n;j++){
                LL sum=0;
                for(int k=1;k<=n;k++)sum+=h[i][k]*t.h[k][j],sum%=Mod;
                b.h[i][j]=sum;
            }
        return b;
    }
    Matrix operator ^ (LL k)const {
        Matrix t=(*this),ans=(*this);
        for(k--;k;k>>=1,t=t*t)
            if(k&1)ans=ans*t;
        return ans;
    }
}a;
istream& operator >> (istream& input,Matrix &x){
    for(int i=1;i<=x.n;i++)
        for(int j=1;j<=x.n;j++)input>>x.h[i][j];
    return input;
}
ostream& operator << (ostream& output,Matrix &x){
    for(int i=1;i<=x.n;i++){
        for(int j=1;j<=x.n;j++)output<<x.h[i][j]<<" ";
        output<<endl;
    }
    return output;
}
int main(){
    ios::sync_with_stdio(false);
    cin>>n>>k;
    a.clear(n);
    cin>>a;
    Matrix ans=a^k;
    cout<<ans;
    return 0;
}

  

posted @ 2017-09-22 17:03  wqtnb_tql_qwq_%%%  阅读(125)  评论(0编辑  收藏  举报