hdu 1575 Tr A (矩阵快速幂模板题)
http://acm.hdu.edu.cn/showproblem.php?pid=1575
题意:A为一方阵,求(A^k)73得到的矩阵的主对角线的和。
思路:矩阵快速幂。模板题。
/* ***********************************************
Author :111qqz
Created Time :2016年02月21日 星期日 10时28分33秒
File Name :code/hdu/1575.cpp
************************************************ */
1#include <cstdio>
2#include <cstring>
3#include <iostream>
4#include <algorithm>
5#include <vector>
6#include <queue>
7#include <set>
8#include <map>
9#include <string>
10#include <cmath>
11#include <cstdlib>
12#include <ctime>
13#define fst first
14#define sec second
15#define lson l,m,rt<<1
16#define rson m+1,r,rt<<1|1
17#define ms(a,x) memset(a,x,sizeof(a))
18typedef long long LL;
19#define pi pair < int ,int >
20#define MP make_pair
1using namespace std;
2const double eps = 1E-8;
3const int dx4[4]={1,0,0,-1};
4const int dy4[4]={0,-1,1,0};
5const int inf = 0x3f3f3f3f;
6const int N=12;
7const int MOD = 9973;
8struct Mat
9{
10 int mat[N][N];
11 void clear()
12 {
13 ms(mat,0);
14 }
15}A;
16int n,k;
1Mat operator * (Mat a,Mat b)
2{
3 Mat c;
4 c.clear();
5 for ( int i = 0 ; i < n ; i++)
6 for ( int j = 0 ; j < n ; j++)
7 for (int k = 0 ; k < n ; k++)
8 c.mat[i][j] =(c.mat[i][j]+a.mat[i][k]*b.mat[k][j])%MOD;
return c;
1}
2Mat operator ^ (Mat a,int b)
3{
4 Mat c;
5 for ( int i = 0 ; i < n ; i++)
6 for ( int j = 0 ; j < n ; j++ )
7 c.mat[i][j]=(i==j);
8 while (b)
9 {
10 if (b&1) c = c * a;
11 b = b>>1;
12 a = a * a;
13 }
14 return c;
15}
16int main()
17{
18 #ifndef ONLINE_JUDGE
19 freopen("code/in.txt","r",stdin);
20 #endif
1 int T;
2 scanf("%d",&T);
3 while (T--)
4 {
5 scanf("%d %d",&n,&k);
6 A.clear();
7 for ( int i = 0 ; i < n ; i++)
8 for ( int j = 0 ; j < n; j ++)
9 scanf("%d",&A.mat[i][j]);
1 Mat res;
2 res.clear();
3 res = A^k;
int ans = 0 ;
for ( int i = 0 ; i < n ;i++) ans = (ans +res.mat[i][i])%MOD;
printf("%d\n",ans);
}
1 #ifndef ONLINE_JUDGE
2 fclose(stdin);
3 #endif
4 return 0;
5}