前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >HDU 1575 Tr A(矩阵快速幂)

HDU 1575 Tr A(矩阵快速幂)

作者头像
attack
发布2018-04-12 11:07:29
2.2K0
发布2018-04-12 11:07:29
举报

Problem Description

A为一个方阵,则Tr A表示A的迹(就是主对角线上各项的和),现要求Tr(A^k)%9973。

Input

数据的第一行是一个T,表示有T组数据。 每组数据的第一行有n(2 <= n <= 10)和k(2 <= k < 10^9)两个数据。接下来有n行,每行有n个数据,每个数据的范围是[0,9],表示方阵A的内容。

Output

对应每组数据,输出Tr(A^k)%9973。

Sample Input

2 2 2 1 0 0 1 3 99999999 1 2 3 4 5 6 7 8 9

Sample Output

2 2686

Author

xhd

Source

HDU 2007-1 Programming Contest

Recommend

linle   |   We have carefully selected several similar problems for you:  1757 1588 2256 2604 2254

矩阵快速幂的裸题!。

我们需要新建一个矩阵,

满足:对角线为1,其余为0

然后跑一下矩阵快速幂就好

代码语言:javascript
复制
 1 #include<cstdio>
 2 #include<cstring>
 3 using namespace std;
 4 const int MAXN=101;
 5 inline void read(int &n){char c='+';bool flag=0;n=0;    
 6 while(c<'0'||c>'9') c=='-'?flag=1,c=getchar():c=getchar();    
 7 while(c>='0'&&c<='9') n=n*10+c-48,c=getchar();flag==1?n=-n:n=n;}
 8 struct matrix
 9 {
10     int m[11][11];matrix(){memset(m,0,sizeof(m));}
11 };
12 matrix ma;
13 int limit;
14 const int mod=9973;
15 matrix mul(matrix a,matrix b)
16 {
17     matrix c;
18     for(int k=0;k<limit;k++)
19         for(int i=0;i<limit;i++)
20             for(int j=0;j<limit;j++)
21                 c.m[i][j]=(c.m[i][j]+(a.m[i][k]*b.m[k][j]))%mod;
22     return c;
23 }
24 matrix fast_martix_pow(matrix ma,int p)
25 {
26     matrix bg;
27     for(int i=0;i<limit;i++)
28         for(int j=0;j<limit;j++)
29             bg.m[i][j]=(i==j);
30     while(p)
31     {
32         if(p&1)    bg=mul(bg,ma);
33         ma=mul(ma,ma);
34         p>>=1;
35     }
36     return bg;
37 }
38 int main()
39 {
40     int T;read(T);
41     while(T--)
42     {
43         read(limit);int n;read(n);
44         for(int i=0;i<limit;i++)
45             for(int j=0;j<limit;j++)
46                 read(ma.m[i][j]);
47         matrix ans=fast_martix_pow(ma,n);
48         int out=0;
49         for(int i=0;i<limit;i++)
50             out+=ans.m[i][i]%mod;
51         printf("%d\n",out%mod);
52     }
53     return 0;
54 }
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017-08-14 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档