hdu 1575 矩阵连乘2

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
const int mod=9973;
int n;
struct matrix
{
  int f[10][10];
};

matrix mul(matrix a,matrix b)
{
    int i,j,k;
    matrix c;
    memset(c.f,0,sizeof(c.f));
    for(k=0;k<n;k++)
    {
         for(i=0;i<n;i++)
      {
            for(j=0;j<n;j++)
          {
            c.f[i][j]+=a.f[i][k]*b.f[k][j];
               c.f[i][j]%=mod;
           }
       }

     }
    return c;
}

matrix pow_mod(matrix a,int b)
{
    matrix s;
    int t;
    memset(s.f,0,sizeof(s.f));
    for(int i=0;i<n;i++)
    {
        s.f[i][i]=1;
    }
    while(b)
    {

        t=b%2;
        b/=2;
        if(t!=0)
            s=mul(s,a);
         a=mul(a,a);
    }
    return s;
}


int main()
{
    int t,k;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&k) ;
        matrix e;
       for(int i=0;i<n;i++)
          for(int j=0;j<n;j++)
          {
              scanf("%d",&e.f[i][j]);
          }

    e=pow_mod(e,k);
    int ans=0;
    for(int i=0;i<n;i++)
        ans=(ans+e.f[i][i])%mod;
    printf("%d
",ans);
    }

    return 0;
}
View Code
原文地址:https://www.cnblogs.com/xiaotian-222/p/5041616.html