首先找到f[n],指数可以通过矩阵快速幂来求解,由于指数较大,需要利用以下公式降幂
得到指数之后,再利用快速幂得到答案。
#include<cstdio> #include<cstring> #include<cmath> #include<algorithm> using namespace std; const long long mod1=1000000006; const long long mod2=1000000007; long long a,b; int n; long long z1,z2; long long mod(long long a, long long b) { if (a >= 0) return a%b; if (abs(a) % b == 0) return 0; return (a + b*(abs(a) / b + 1)); } struct Matrix { long long A[5][5]; int R, C; Matrix operator*(Matrix b); }; Matrix X, Y, Z; Matrix Matrix::operator*(Matrix b) { Matrix c; memset(c.A, 0, sizeof(c.A)); int i, j, k; for (i = 1; i <= R; i++) for (j = 1; j <= C; j++) for (k = 1; k <= C; k++) c.A[i][j] = mod((c.A[i][j] + mod(A[i][k] * b.A[k][j], mod1)), mod1); c.R=R; c.C=b.C; return c; } void init() { Z.A[1][1] = 0, Z.A[1][2] = 1; Z.R = 1; Z.C = 2; Y.A[1][1] = 1, Y.A[1][2] = 0, Y.A[2][1] = 0, Y.A[2][2] = 1; Y.R = 2; Y.C = 2; X.A[1][1] = 0, X.A[1][2] = 1, X.A[2][1] = 1, X.A[2][2] = 1; X.R = 2; X.C = 2; } void work1(int x) { while (x) { if (x % 2 == 1) Y = Y*X; x = x >> 1; X = X*X; } Z = Z*Y; z2=mod(Z.A[1][1], mod1); // printf("%lld ", mod(Z.A[1][1], mod1)); } void work2(int x) { while (x) { if (x % 2 == 1) Y = Y*X; x = x >> 1; X = X*X; } Z = Z*Y; z1=mod(Z.A[1][1], mod1); // printf("%lld ", mod(Z.A[1][1], mod1)); } long long quickmod(long long a,long long b,long long m) { long long ans = 1; while(b) { if(b&1) { ans = (ans*a)%m; b--; } b/=2; a = a*a%m; } return ans; } int main() { while(~scanf("%lld%lld%d",&a,&b,&n)) { if(n==0) printf("%lld ",a%mod2); else if(n==1) printf("%lld ",b%mod2); else { init(); work1(n); init(); work2(n-1); printf("%lld ",(quickmod(a,z1,mod2)*quickmod(b,z2,mod2))%mod2); } } return 0; }