您的位置:首页 > 其它

【HDU 1575 Tr A】+ 矩阵快速幂

2016-11-06 19:01 363 查看
Tr A

Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 4501 Accepted Submission(s): 3386

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

矩阵快速幂~~~

AC代码 :

#include<bits/stdc++.h>
using namespace std;
const int mod = 9973;
struct node{
int m[12][12];
}dw,ans;
int N,K;
node JZ(node a,node b){
node x;
for(int i = 0 ; i < N; i++)
for(int j = 0 ; j < N ; j++){
x.m[i][j] = 0;
for(int k = 0 ; k < N ; k++)
x.m[i][j] = (x.m[i][j] + a.m[i][k] * b.m[k][j]) % mod;
}
return x;
}
node solve(int x){
while(x){
if(x & 1)
ans = JZ(ans,dw);
dw = JZ(dw,dw);
x >>= 1;
}
return ans;
}
int main()
{
int T,a;
scanf("%d",&T);
while(T--){
scanf("%d %d",&N,&K);
for(int i = 0 ; i < N ; i++)
for(int j = 0 ; j < N ; j++){
scanf("%d",&a);
dw.m[i][j] = ans.m[i][j] = a;
}
node cut = solve(--K);
int sum = 0;
for(int i = 0 ; i < N; i++)
sum = (sum + cut.m[i][i]) % mod;
printf("%d\n",sum);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: