HUD - 1575 - Tr A ( 矩阵快速幂,简单题目 )
2016-12-12 20:34
344 查看
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
#include<cstdio>
#include<cstring>
#define LL long long
#define MOD 9973
using namespace std;
int n,k,t;
struct Matrix{
LL m[11][11];
Matrix(){memset(m,0,sizeof(m));}
};
Matrix Mul(Matrix x ,Matrix y){
Matrix ans;
for(int i=0 ;i<n ;i++){
for(int j=0 ;j<n ;j++){
for(int k=0 ;k<n ;k++){
ans.m[i][j] = ( ans.m[i][j] + x.m[i][k] * y.m[k][j] )%MOD;
}
}
}
return ans;
}
Matrix q_pow(Matrix x,int k){
Matrix ans;
for(int i=0 ;i<n ;i++) ans.m[i][i]=1;
while(k){
if(k&1) ans = Mul(ans,x);
x = Mul(x,x);
k >>= 1;
}
return ans;
}
int main(){
scanf("%d",&t);
while(t--){
scanf("%d%d",&n,&k);
Matrix mat;
for(int i=0 ;i<n ;i++){
for(int j=0 ;j<n ;j++){
scanf("%lld",&mat.m[i][j]);
}
}
Matrix res = q_pow(mat,k);
LL ans = 0;
for(int i=0 ;i<n ;i++) ans = (ans+res.m[i][i])%MOD;
printf("%lld\n",ans);
}
return 0;
}
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
#include<cstdio>
#include<cstring>
#define LL long long
#define MOD 9973
using namespace std;
int n,k,t;
struct Matrix{
LL m[11][11];
Matrix(){memset(m,0,sizeof(m));}
};
Matrix Mul(Matrix x ,Matrix y){
Matrix ans;
for(int i=0 ;i<n ;i++){
for(int j=0 ;j<n ;j++){
for(int k=0 ;k<n ;k++){
ans.m[i][j] = ( ans.m[i][j] + x.m[i][k] * y.m[k][j] )%MOD;
}
}
}
return ans;
}
Matrix q_pow(Matrix x,int k){
Matrix ans;
for(int i=0 ;i<n ;i++) ans.m[i][i]=1;
while(k){
if(k&1) ans = Mul(ans,x);
x = Mul(x,x);
k >>= 1;
}
return ans;
}
int main(){
scanf("%d",&t);
while(t--){
scanf("%d%d",&n,&k);
Matrix mat;
for(int i=0 ;i<n ;i++){
for(int j=0 ;j<n ;j++){
scanf("%lld",&mat.m[i][j]);
}
}
Matrix res = q_pow(mat,k);
LL ans = 0;
for(int i=0 ;i<n ;i++) ans = (ans+res.m[i][i])%MOD;
printf("%lld\n",ans);
}
return 0;
}
相关文章推荐
- hdu 1575 Tr A(矩阵快速幂,简单)
- hdu1575 Tr A(矩阵快速幂)
- hdoj 1575 Tr A 【矩阵快速幂】
- hdoj 1575 Tr A 【矩阵快速幂】
- HDU 1575 Tr A(矩阵快速幂)
- hdu 1575 Tr A(矩阵乘法快速求幂)
- HDU - 1575 Tr A 纯矩阵快速幂
- hdu 1575 Tr A(矩阵快速幂)
- HDU - 1575 Tr A(矩阵快速幂)
- Hdu 1575 Tr A【矩阵快速幂】
- HDU 1575 Tr A 矩阵快速幂
- hdu 1575 Tr A(裸的矩阵快速幂)
- HDU 1575 Tr A (矩阵快速幂)
- 杭电1575(矩阵快速幂)Tr A
- HDOJ 题目1575 Tr A(矩阵相乘,二分)
- hdu 1575 Tr A(矩阵快速幂入门)
- 杭电1575-Tr A(矩阵快速幂)
- HDU 1575 Tr A (矩阵快速幂入门)
- hdu 1575 Tr A(矩阵快速幂乘法优化算法)
- hdu 1575 Tr A (矩阵快速幂入门题)