其实跟普通的快速幂类似,只是普通乘法换成了矩阵乘法,所以时间复杂度为 $O(k^3logn)$($k$为矩阵大小)
#include<bits/stdc++.h> using namespace std; typedef long long ll; typedef vector<ll> vec; typedef vector<vec> mat; const int mod = 1000000000 + 7; void init(mat A) { for(int i = 0;i < A.size();i++) A[i].clear(); } //计算A*B%mod mat mul(mat&A, mat &B) { mat C(A.size(), vec(B[0].size())); for(int i = 0;i < A.size();i++) for(int k = 0;k < B[0].size();k++) for(int j = 0;j < B[0].size();j++) C[i][j] = (C[i][j] + A[i][k] * B[k][j]) % mod; //矩阵的各元素对mod取余 return C; } //计算A^n mat pow(mat A, ll n) { mat B(A.size(), vec(A.size())); //?? 能自身与自身相乘,肯定是方阵 for(int i = 0;i < A.size();i++) B[i][i] = 1; while(n > 0) { if(n & 1) B = mul(B, A); A = mul(A, A); n >>= 1; } return B; } ll n; int main() { while(scanf("%lld", &n) == 1) { mat A(2, vec(2)), B(2, vec(1)); init(A); init(B); A[0][0] = 7; A[0][1] = 0; A[1][0] = 1; A[1][1] = 1; B[0][0] = 1; B[1][0] = 0; A = pow(A, n); A = mul(A, B); printf("%lld %lld ", A[0][0], A[1][0]); } return 0; }