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 }
再附加一个博文链接:传送门