这道题要小小的开下脑洞~
还有必须知道费马小定理。
fn = a(Sn-1)*b(Sn-2),Sn为第n项斐波拉契数列~
这样就先用矩阵快速幂求n项斐波拉契数列,之后再根据费马小定理,a^n = a^(n%mod-1) ,mod必须是素数。
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
using namespace std;
#define LL long long //数据比较大,最好都定义成long long
const int mod = 1e9+7;
struct matrix {
LL m[3][3];
matrix() {
memset(m,0,sizeof(m));
}
};
matrix mul(matrix a,matrix b) {
matrix tmp;
for(int i = 1;i <= 2;i++)
for(int j = 1;j <= 2;j++)
for(int k = 1;k <= 2;k++)
tmp.m[i][j] = (tmp.m[i][j] + ((a.m[i][k] % (mod-1)) * (b.m[k][j] % (mod-1)) % (mod - 1)));
return tmp;
}
matrix powmul(matrix a,int n) {
matrix tmp;
for(int i = 1;i <= 2;i++)
tmp.m[i][i] = 1;
while(n) {
if(n & 1)
tmp = mul(a,tmp);
a = mul(a,a);
n >>= 1;
}
return tmp;
}
LL fast_pow(LL x,LL n) {
LL tmp = 1;
while(n) {
if(n & 1)
tmp = (tmp % mod) * (x % mod) % mod;
x = (x % mod)* (x % mod) % mod;
n >>= 1;
//printf("%d\n",tmp);
}
return tmp % mod;
}
int main() {
int a,b,n;
while(scanf("%d%d%d",&a,&b,&n) != EOF) {
if(n == 0)
printf("%d\n",a);
else if(n == 1)
printf("%d\n",b);
else {
matrix base,ans;
base.m[1][1] = base.m[1][2] = base.m[2][1] = 1;
ans = powmul(base,n-1);
LL x = ans.m[1][1];
LL y = ans.m[1][2];
// printf("%lld %lld\n",x,y);
LL ans1 = fast_pow(a,y);
LL ans2 = fast_pow(b,x);
printf("%lld\n",(ans1 * ans2) % mod);
}
}
}