矩阵快速幂板子

 1 struct Matrix
 2 {
 3     int a[3][3];
 4     Matrix()
 5     {
 6         memset(a,0,sizeof(a));
 7     }
 8     void init()
 9     {
10         for(int i=0;i<3;i++)
11             for(int j=0;j<3;j++)
12                 a[i][j]=(i==j);
13     }
14     Matrix operator * (const Matrix &B)const
15     {
16         Matrix C;
17         for(int i=0;i<3;i++)
18             for(int j=0;j<3;j++)
19                 for(int k=0;k<3;k++)
20                     C.a[i][j]=(C.a[i][j]+1LL*a[i][k]*B.a[k][j])%Mod;
21         return C;
22     }
23     Matrix operator ^ (const ll &p)const
24     {
25         Matrix A=(*this),res;
26         res.init();
27         ll t=p;
28         while(t)
29         {
30             if(t&1)res=res*A;
31             A=A*A;
32             t>>=1;
33         }
34         return res;
35     }
36 }M[8];

 

posted @ 2018-04-08 20:25  NotNight  阅读(227)  评论(0编辑  收藏  举报