hdu1575TrA(矩阵快速幂入门)

传送门

ac代码:

#include<bits/stdc++.h>
#define per(i,a,b) for(int i=a;i<=b;i++)
#define mod 9973
using namespace std;
typedef long long ll;
//#define int long long
const int inf =0x3f3f3f3f;
const double eps=1e-8;
int read(){
    char ch=getchar();
    int res=0,f=0;
    while(ch<'0' || ch>'9'){f=(ch=='-'?-1:1);ch=getchar();}
    while(ch>='0'&&ch<='9'){res=res*10+(ch-'0');ch=getchar();}
    return res*f;
}
// ------------------------head
const int siz=1000005;
int T,n,k;
struct mat{
    int a[13][13];
    mat(){memset(a,0,sizeof(a));}
    mat operator*(mat rhs){
        mat res;
        per(i,0,n-1)per(j,0,n-1){
            per(k,0,n-1){
                res.a[i][j]=(res.a[i][j]+a[i][k]*rhs.a[k][j]%mod)%mod;
            }
        } 
        return res;
    }
}m1,m2;
mat m_pow(mat a,int k){
    mat res;
    per(i,0,n-1)res.a[i][i]=1;
    while(k){
        if(k&1)res=res*a;
        a=a*a;
        k/=2;
    }
    return res;
}
signed main()
{
    scanf("%d",&T);
    while(T--){
        scanf("%d%d",&n,&k);
        per(i,0,n-1)per(j,0,n-1)scanf("%d",&m1.a[i][j]);
        m2=m_pow(m1,k);
        int res=0;
        per(i,0,n-1)res=(res+m2.a[i][i])%mod;
        printf("%d\n",res);
    }

    return 0;
}

 

posted @ 2018-09-14 14:26  WindFreedom  阅读(351)  评论(0编辑  收藏  举报