Pentium.Labs

System全家桶:https://zhuanlan.zhihu.com/c_1238468913098731520

导航

hdu 1575 矩阵快速幂模板

 1 #include "iostream"
 2 #include "vector"
 3 #include "cstring"
 4 using namespace std;
 5 
 6 typedef unsigned long int ULL;
 7 typedef vector<ULL> vec;
 8 typedef vector<vec> mat;
 9 const ULL P=9973;
10 int n,m;
11 
12 mat mul(mat &A,mat &B)      //return A*B
13 {
14     mat C(A.size(),vec(B[0].size()));
15     for (int i=0;i<(int)A.size();i++)
16     {
17         for (int k=0;k<(int)B.size();k++)
18         {
19             for (int j=0;j<(int)B[0].size();j++)
20             {
21                 C[i][j]=(C[i][j]+A[i][k]*B[k][j])%P;
22             }
23         }
24     }
25     return C;
26 }
27 
28 mat m_pow(mat A,int m)      //return A^m
29 {
30     mat B(A.size(),vec(A.size()));
31     for (int i=0;i<(int)A.size();i++)
32         B[i][i]=1;
33     while (m>0)
34     {
35         if (m&1)    B=mul(B,A);
36         A=mul(A,A);
37         m>>=1;
38     }
39     return B;
40 }
41 
42 int main()
43 {
44     int T;
45     cin>>T;
46     while (T--)
47     {
48         cin>>n>>m;
49         mat A(n,vec(n));
50         for (int i=0;i<n;i++)
51             for (int j=0;j<n;j++)
52                 cin>>A[i][j];
53 
54         A=m_pow(A,m);
55 
56         ULL ans=0;
57         for (int i=0;i<n;i++)
58         {
59             ans+=A[i][i];
60             ans=ans%P;
61         }
62         cout<<ans<<endl;
63     }
64     return 0;
65 }

 

posted on 2014-10-30 12:48  Pentium.Labs  阅读(178)  评论(0编辑  收藏  举报



Pentium.Lab Since 1998