Description
Input
Output
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 <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int MOD = 9973; struct Matrix { int arr[12][12]; }init, unit; int n,k; Matrix Mul(Matrix a, Matrix b) { Matrix c; for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) { c.arr[i][j] = 0; for (int k = 0; k < n; k++) c.arr[i][j] = (c.arr[i][j]+(a.arr[i][k]*b.arr[k][j])%MOD)%MOD; } return c; } Matrix Pow(Matrix a, Matrix b, int x) { while (x) { if (x & 1) b = Mul(b, a); x >>= 1; a = Mul(a, a); } return b; } int main() { int t; 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", &init.arr[i][j]); unit.arr[i][j] = init.arr[i][j]; } Matrix res = Pow(init, unit, k-1); int ans = 0; for (int i = 0; i < n; i++) ans = (ans + res.arr[i][i]) % MOD; printf("%d\n", ans%MOD); } return 0; }
HDU - 1575 Tr A,布布扣,bubuko.com
原文:http://blog.csdn.net/u011345136/article/details/36187929