矩阵快速幂

 1 //矩阵间的乘法%m
 2 Matrix matrix_mul(Matrix A, Matrix B, long long m)
 3 {
 4     Matrix ans;
 5 
 6     for (int i = 0; i < N; i++)
 7     {
 8         for (int j = 0; j < N; j++)
 9         {
10             ans.v[i][j] = 0;
11             for (int k = 0; k < N; k++)
12             {
13                 ans.v[i][j] += (A.v[i][k] * B.v[k][j]) % m;
14             }
15             ans.v[i][j] %= m;
16         }
17     }
18     return ans;
19 }
20 
21 //矩阵快速幂c^n%m
22 Matrix matrix_pow(Matrix C, long long n, long long m)
23 {
24     Matrix ans = { 1, 0, 0, 1 };//e
25     while (n)
26     {
27         if (n & 1)
28             ans = matrix_mul(ans, C, m);
29         C = matrix_mul(C, C, m);
30         n >>= 1;
31     }
32     return ans;
33 }

 

posted @ 2018-04-06 14:16  ouyang_wsgwz  阅读(137)  评论(0编辑  收藏  举报