矩阵快速幂

#include <iostream>
#include <cstdio>
#include <queue>
#include <algorithm>
#include <cmath>
#include <cstring>
#define inf 2147483647
#define N 1000010
#define mod 1000000007
#define p(a) putchar(a)
#define For(i,a,b) for(long long i=a;i<=b;++i)
//by war
//2019.7.31
using namespace std;
long long n,k;
struct matrix{
    long long a[110][110];
    matrix ksm(matrix a,long long b){
        matrix r=a;b--;
        while(b>0){
            if(b&1)
               r=r*a;
           a=a*a;
           b>>=1;
        }
        return r;
    }
    matrix operator *(const matrix&b)const{
        matrix r;
        For(i,1,n)
          For(j,1,n){
                r.a[i][j]=0;
                For(k,1,n)
                r.a[i][j]+=a[i][k]*b.a[k][j]%mod;
                r.a[i][j]%=mod;
            }
        return r;
    }  
}a;

void in(long long &x){
    long long y=1;char c=getchar();x=0;
    while(c<'0'||c>'9'){if(c=='-')y=-1;c=getchar();}
    while(c<='9'&&c>='0'){ x=(x<<1)+(x<<3)+c-'0';c=getchar();}
    x*=y;
}
void o(long long x){
    if(x<0){p('-');x=-x;}
    if(x>9)o(x/10);
    p(x%10+'0');
}

int main(){
    in(n);in(k);
    For(i,1,n)
      For(j,1,n)
        in(a.a[i][j]);  
    matrix r;
    r=r.ksm(a,k);
    For(i,1,n){    
      For(j,1,n)
        o(r.a[i][j]),p(' ');
        p('\n');    
    }
     return 0;
}

 

posted @ 2017-10-26 15:38  WeiAR  阅读(131)  评论(0编辑  收藏  举报