广义肥波 欧拉降幂
1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 const int maxn=1e5+10; 5 const ll mod=1e9+7; 6 ll f[maxn]; 7 ll quick(ll a,ll b) 8 { 9 ll sum=1; 10 while(b){ 11 if(b%2) sum=(sum*a)%mod; 12 a=a*a%mod; 13 b=b/2; 14 } 15 return sum; 16 } 17 int main() 18 { 19 ll a,b,m,n; 20 scanf("%lld%lld%lld%lld",&a,&b,&m,&n); 21 f[1]=1; 22 f[2]=1; 23 for(int i=3;i<=n;i++){ 24 f[i]=(a*f[i-1]+b*f[i-2])%(mod-1); 25 } 26 // printf("%d\n",f[n]); 27 ll ans=quick(m,f[n]); 28 printf("%lld\n",ans); 29 return 0; 30 }