hdu1575---Tr A,hdu1575---Tr
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
水題,矩陣快速冪
/************************************************************************* > File Name: hdu1575.cpp > Author: ALex > Mail: zchao1995@gmail.com > Created Time: 2015年03月10日 星期二 19時20分38秒 ************************************************************************/#include <map>#include <set>#include <queue>#include <stack>#include <vector>#include <cmath>#include <cstdio>#include <cstdlib>#include <cstring>#include <iostream>#include <algorithm>using namespace std;const double pi = acos(-1);const int inf = 0x3f3f3f3f;const double eps = 1e-15;typedef long long LL;typedef pair <int, int> PLL;const int mod = 9973;int n;struct MARTIX{ int mat[15][15];};MARTIX mul (MARTIX a, MARTIX b){ MARTIX c; for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { c.mat[i][j] = 0; for (int k = 0; k < n; ++k) { c.mat[i][j] += a.mat[i][k] * b.mat[k][j]; c.mat[i][j] %= mod; } } } return c;}void fastpow (MARTIX ret, int k){ MARTIX ans; for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { ans.mat[i][j] = (i == j); } } while (k) { if (k & 1) { ans = mul (ans, ret); } ret = mul (ret, ret); k >>= 1; } int res = 0; for (int i = 0; i < n; ++i) { res += ans.mat[i][i]; res %= mod; } printf("%d\n", res);}int main (){ int t; scanf("%d", &t); while (t--) { int k; scanf("%d%d", &n, &k); MARTIX a; for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { scanf("%d", &a.mat[i][j]); } } fastpow(a, k); } return 0;}