HDU 1575 Tr A (矩阵快速幂)
来源:互联网 发布:微信小程序 源码下载 编辑:程序博客网 时间:2024/05/18 00:46
Tr A
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3159 Accepted Submission(s): 2358
Problem Description A为一个方阵,则Tr A表示A的迹(就是主对角线上各项的和),现要求Tr(A^k)%9973。
数据的第一行是一个T,表示有T组数据。
每组数据的第一行有n(2 <= n <= 10)和k(2 <= k < 10^9)两个数据。接下来有n行,每行有n个数据,每个数据的范围是[0,9],表示方阵A的内容。
每组数据的第一行有n(2 <= n <= 10)和k(2 <= k < 10^9)两个数据。接下来有n行,每行有n个数据,每个数据的范围是[0,9],表示方阵A的内容。
对应每组数据,输出Tr(A^k)%9973。
22 21 00 13 999999991 2 34 5 67 8 9
22686
Author
xhd
HDU 2007-1 Programming Contest
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1575
题目分析:裸的矩阵快速幂
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1575
题目分析:裸的矩阵快速幂
#include <cstdio> #include <cstring> #include <algorithm> using namespace std; int const MOD = 9973; struct matrix { int m[15][15]; }a; int n;matrix multiply(matrix x, matrix y) { matrix ans; memset(ans.m, 0, sizeof(ans.m)); for(int i = 1; i <= n; i++) for(int j = 1; j <= n; j++) if(x.m[i][j]) for(int k = 1; k <= n; k++) ans.m[i][k] = (ans.m[i][k] + x.m[i][j] * y.m[j][k]) % MOD; return ans; } matrix quickmod(matrix a, int p) { matrix ans; memset(ans.m, 0, sizeof(ans.m)); for(int i = 1; i <= n; i++) ans.m[i][i] = 1; while(p) { if(p & 1) ans = multiply(ans, a); p >>= 1; a = multiply(a, a); } return ans; } int main(){ int T; scanf("%d", &T); while(T--) { int k, res = 0; scanf("%d %d", &n, &k); matrix ans; for(int i = 1; i <= n; i++) for(int j = 1; j <= n; j++) scanf("%d", &ans.m[i][j]); ans = quickmod(ans, k); for(int i = 1; i <= n; i++) res = (res + ans.m[i][i]) % MOD; printf("%d\n", res); } }
0 0
- hdu 1575Tr 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 (矩阵快速幂)
- 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(矩阵快速幂)
- 1592 - Database
- 10474 - Where is the Marble
- 10815 - Andy's First Dictionary
- 12096 - The SetStack Computer
- 212 - Use of Hospital Facilities
- HDU 1575 Tr A (矩阵快速幂)
- UVa 11375 Matches
- 230 - Borrowers
- 511 - Do You Know the Way to San Jose
- 822 - Queue and A
- PHP中错误控制@运算符
- 1593 - Alignment of Code
- 编程笔记_J2SE_LinkList&ArrayList&HashMap
- 1594 - Ducci Sequence