Tr A

来源:互联网 发布:python 矩阵相乘 编辑:程序博客网 时间:2024/06/05 09:39

Tr A
Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u
Submit Status Practice HDU 1575

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

22 21 00 13 999999991 2 34 5 67 8 9
 

Sample Output

22686
 


一道简单的矩阵快速幂。。。

         话说刚开始没看懂,语文功底太差了。。。。。




#include <iostream>#include <cstdio>#include <cstring>#define Mod 9973struct Node{  int m[12][12];  int x;  int y;}pt,st , dt;Node  Pow(Node a, Node b){    memset(dt.m , 0 ,sizeof(dt.m));      dt.x = a.x;    dt.y = b.y;  for(int i = 1; i <= a.x; i++)    {      for(int k = 1; k <= a.y; k++ )       {             if(a.m[i][k] == 0)               continue;               for(int j = 1; j <= b.y; j++)               {                dt.m[i][j] = ((dt.m[i][j] + a.m[i][k] * b.m[k][j]) % Mod) % Mod;   }   } }return dt;}void QuickPow(int n){while(n) {      if( n % 2 == 1)       {            pt = Pow( pt , st); }   n = n / 2;  st = Pow(st , st); }}int main(){     int n,k,t;   scanf("%d",&t);   while(t--)   {   scanf("%d %d",&n,&k);   memset(pt.m, 0 ,sizeof(pt.m));   memset(st.m , 0 , sizeof(st.m));    for(int i = 1; i <= n; i++)       for(int j = 1; j <= n; j++)            scanf("%d",&st.m[i][j]);           for(int i = 1; i <= n; i++)                 pt.m[i][i]  = 1;  //单位矩阵             pt.x = pt.y = n;             st.x = st.y = n;          int sum = 0;          QuickPow(k);          for(int i = 1; i <= n; i++)           {              sum += pt.m[i][i];   }   printf("%d\n",sum%Mod);  }return 0;}







原创粉丝点击