标签:
题目:
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
直接把(A^k)%9973求出来,然后求它的迹,最后取模即可。
很容易证明,Tr(A^k)%9973=Tr((A^k)%9973)%9973
代码:
#include<iostream> using namespace std; int n, k; int in[10][10]; int list[10][10]; int temp[10][10]; void pro(int l1[][10], int l2[][10], int t[][10]) //t=l1*l2 { for (int i = 0; i < n; i++)for (int j = 0; j < n; j++) { t[i][j] = 0; for (int k = 0; k < n; k++)t[i][j] = (t[i][j] + l1[i][k] * l2[k][j]) % 9973; } } void f(int num) //让list是in的n次方 { if (num == 0) { for (int i = 0; i < n; i++)for (int j = 0; j < n; j++)list[i][j] = (i == j); return; } f(num / 2); pro(list, list, temp); if (num % 2)pro(temp, in, list); else for (int i = 0; i < n; i++)for (int j = 0; j < n; j++)list[i][j] = temp[i][j]; } int get_tr() { int sum = 0; for (int i = 0; i < n; i++)sum += list[i][i]; return sum; } int main() { int cas; cin >> cas; while (cas--) { cin >> n >> k; for (int i = 0; i < n; i++)for (int j = 0; j < n; j++)cin >> in[i][j]; f(k); cout << get_tr() % 9973 << endl; } return 0; }
标签:
原文地址:http://blog.csdn.net/nameofcsdn/article/details/52259384