Educational Codeforces Round 13 D. Iterated Linear Function (矩阵快速幂)

题目链接:http://codeforces.com/problemset/problem/678/D

简单的矩阵快速幂模版题

矩阵是这样的:234

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 typedef __int64 LL;
 4 struct data {
 5     LL mat[3][3];
 6 };
 7 LL mod = 1e9 + 7;
 8 
 9 data operator *(data a , data b) {
10     data res;
11     for(int i = 1 ; i <= 2 ; ++i) {
12         for(int j = 1 ; j <= 2 ; ++j) {
13             res.mat[i][j] = 0;
14             for(int k = 1 ; k <= 2 ; ++k) {
15                 res.mat[i][j] = (a.mat[i][k] * b.mat[k][j] % mod + res.mat[i][j]) % mod;
16             }
17         }
18     }
19     return res;
20 }
21 
22 data operator ^(data a , LL n) {
23     data res;
24     for(int i = 1 ; i <= 2 ; ++i) {
25         for(int j = 1 ; j <= 2 ; ++j) {
26             res.mat[i][j] = i == j;
27         }
28     }
29     while(n) {
30         if(n & 1)
31             res = res * a;
32         a = a * a;
33         n >>= 1;
34     }
35     return res;
36 }
37 
38 int main()
39 {
40     LL a , b , n , x;
41     cin >> a >> b >> n >> x;
42     data temp , res;
43     temp.mat[1][1] = a % mod , temp.mat[1][2] = 0 , temp.mat[2][1] = b % mod, temp.mat[2][2] = 1;
44     res.mat[1][1] = x % mod , res.mat[1][2] = 1 , res.mat[2][1] = res.mat[2][2] = 0;
45     temp = temp ^ n;
46     res = res * temp;
47     cout << res.mat[1][1] << endl;
48     return 0;
49 }

 

posted @ 2016-06-20 16:50  Recoder  阅读(187)  评论(0编辑  收藏  举报