题意:
F[0] = a
F[1] = b
F[n] = F[n-1] * F[n-2] ( n > 1 )
求F(N)
思路:
没有直接构造出来矩阵,就把乘法转化成了幂的加法
然后就比较容易构造矩阵了
F(n)=F(n-1)+F(n-2)+1
注意一点是取模的时候因为是指数取模,所以MOD 1e9+6
1e9+6=phi(1e9+7) 这里是欧拉函数的知识
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<math.h>
#include<queue>
#include<stack>
#include<string>
#include<vector>
#include<map>
#include<set>
using namespace std;
#define lowbit(x) (x&(-x))
typedef long long LL;
const int maxn = 100005;
const int inf=(1<<28)-1;
#define Matrix_Size 5
const LL MOD = 1e9+6;
int Size;
struct Matrix
{
LL mat[Matrix_Size][Matrix_Size];
void clear()
{
memset(mat,0,sizeof(mat));
}
void output()
{
for(int i = 0;i < Size;i++)
{
for(int j = 0;j < Size;j++)
printf("%lld ",mat[i][j]);
printf("\n");
}
}
Matrix operator *(const Matrix &b)const
{
Matrix ret;
for(int i = 0;i < Size;i++)
for(int j = 0;j < Size;j++)
{
ret.mat[i][j] = 0;
for(int k = 0;k < Size;k++)
{
long long tmp = (long long)mat[i][k]*b.mat[k][j]%MOD;
ret.mat[i][j] = (ret.mat[i][j]+tmp);
if(ret.mat[i][j]>=MOD)
ret.mat[i][j] -= MOD;
if(ret.mat[i][j]<0)//注意是否需要MOD
ret.mat[i][j] += MOD;
}
}
return ret;
}
};
Matrix pow_M(Matrix a,long long n)
{
Matrix ret;
ret.clear();
for(int i = 0;i < Size;i++)
ret.mat[i][i] = 1;
Matrix tmp = a;
while(n)
{
if(n&1)ret = ret*tmp;
tmp = tmp*tmp;
n>>=1;
}
return ret;
}
LL quickMod(LL a,LL b,LL mod)
{
LL res=1,tmp=a;
while(b)
{
if(b&1)
res=(res*tmp)%mod;
tmp=(tmp*tmp)%mod;
b/=2;
}
return res;
}
int main()
{
LL a,b,n;
while(~scanf("%lld%lld%lld",&a,&b,&n))
{
Size=4;
Matrix A,B;
A.clear(),B.clear();
A.mat[0][0]=A.mat[0][2]=1;
A.mat[1][1]=A.mat[1][3]=1;
A.mat[2][0]=A.mat[3][1]=1;
B.mat[1][0]=B.mat[2][0]=1;
if(n==0)
{
printf("%lld\n",a%(MOD+1));
continue;
}
if(n==1)
{
printf("%lld\n",b%(MOD+1));
continue;
}
A=pow_M(A,n-1);
A=A*B;
//A.output();
LL Ans=(quickMod(a,A.mat[0][0],MOD+1)*quickMod(b,A.mat[1][0],MOD+1))%(MOD+1);
printf("%lld\n",Ans);
}
return 0;
}