Let ??=?2?−6⋅??−1⋅??−2⋅??−3fx=c2x−6⋅fx−1⋅fx−2⋅fx−3 for ?≥4x≥4.
You have given integers ?n, ?1f1, ?2f2, ?3f3, and ?c. Find ??mod(109+7)fnmod(109+7).
Input
The only line contains five integers ?n, ?1f1, ?2f2, ?3f3, and ?c (4≤?≤10184≤n≤1018, 1≤?11≤f1, ?2f2, ?3f3, ?≤109c≤109).
思路 :原式可化为: c^x* ??=?^(?−1)⋅??−1⋅c^(x-2)^??−2⋅c^(x-3)⋅??−3 因此设gx=c^x⋅fx。因此原式转化成gx=gx-1⋅gx-2⋅gx-3
我们只看每一项的指数 ,每一项指数满足:xn=xn-1+xn-2+xn-3 因此本题转化为矩阵快速幂的模版题 算出对应的幂次 矩阵快速幂详解 注意这里不能直接对指数取膜 详见:欧拉降幂
#include <iostream>
#include <cstdio>
#define ll long long
#define rep(i,x,y) for(int i=(x);i<=(y);++i)
using namespace std;
const int N=1e6+10;
const int mo=1e9+7;
//矩阵
struct mt{
ll a[3][3];
ll* operator [](int x){return a[x];}
const ll* operator [](int x)const{return a[x];}
friend mt operator *(const mt&a,const mt&b){mt c;
for(int i=0;i<=2;i++)
for(int j=0;j<=2;j++)
c[i][j]=(a[i][0]*b[0][j]+a[i][1]*b[1][j]+a[i][2]*b[2][j])%(mo-1);
return c;
}
};
//矩阵快速幂
mt qk(mt r,mt x,ll n){
for(;n;n>>=1){
if(n&1)r=r*x;x=x*x;
}return r;
}
ll mod;
ll qpow(ll a, ll n)
{
ll re = 1;
while(n)
{
if(n & 1)
re = (re * a) % mo;
n >>= 1;
a = (a * a) % mo;
}
return re % mo;
}
int main(){
ll n,f1,f2,f3,c;
ll g1,g2,g3;
scanf("%lld%lld%lld%lld%lld",&n,&f1,&f2,&f3,&c);
g1=f1*c%mo;
g2=f2*c%mo*c%mo;
g3=f3*c%mo*c%mo*c%mo;
int res1[][3]={{1,0,0},{0,1,0},{0,0,1}};
int res2[][3]={{1,1,0},{1,0,1},{1,0,0}};
mt r1,r2;
for(int i=0;i<=2;i++){
for(int j=0;j<=2;j++){
r1.a[i][j]=res1[i][j];
r2.a[i][j]=res2[i][j];
}
}
mt res=qk(r1,r2,n-3);
ll gn=qpow(g1,res[2][0])*qpow(g2,res[1][0])%mo*qpow(g3,res[0][0])%mo;
printf("%lld\n",gn*qpow(qpow(c,n),mo-2)%mo);
}