码迷,mamicode.com
首页 > 其他好文 > 详细

矩阵快速幂模板

时间:2016-06-14 23:44:04      阅读:129      评论:0      收藏:0      [点我收藏+]

标签:

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 typedef long long LL;
 4 const int Q = 1e9 + 7;
 5 struct Matrix {
 6     int n , m , a[2][2];
 7     Matrix (int _n = 0, int _m = 0) {
 8         n = _n , m = _m;
 9         memset(a , 0 , sizeof(a));
10     }
11     Matrix operator * (const Matrix &R) const {
12         Matrix res(n , R.m);
13         for (int i = 0 ; i < n ; ++ i) {
14             for (int j = 0 ; j < m ; ++ j) {
15                 for (int k = 0 ; k < R.m ; ++ k) {
16                     res.a[i][k] += (LL)a[i][j] * R.a[j][k] % Q;
17                     res.a[i][k] %= Q;
18                 }
19             }
20         }
21         return res;
22     }
23 };
24 
25 int main() {
26     LL A , B , n , x;
27     cin >> A >> B >> n >> x;
28     Matrix I(1 , 2);
29     I.a[0][0] = x , I.a[0][1] = 1;
30     Matrix P(2 , 2);
31     P.a[0][0] = A;
32     P.a[1][0] = B;
33     P.a[1][1] = 1;
34     while (n) {
35         if (n & 1) {
36             I = I * P;
37         }
38         P = P * P;
39         n >>= 1;
40     }
41     cout << I.a[0][0] << endl;
42 }

 

矩阵快速幂模板

标签:

原文地址:http://www.cnblogs.com/Kurokey/p/5585722.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!