Generalized fat wave
Title link: nowcoder 214267
To Niu Ke:
Topic
Define the generalized Fibonacci sequence:
{f 1 = 1 f 2 = 1 fi = a × fi − 1 + b × fi − 2 (n ≥ 3, n ∈ Z) \left\{\begin{matrix} f_1= 1\\ f_2=1\\ f_i=a\times f_{i-1}+b\times f_{i-2}(n\geq3,n\in \mathbb{Z}) \end{matrix}\right .⎩⎨⎧f1=1f2=1fi=a×fi−1+b×fi−2(n≥3,n∈With )
Then let you find mfn% (1 e 9 + 7) m^(f_n) \%(1e9+7)mfn%(1e9+7)
Ideas
Seeing the scope of the title, we can see that it should enumerate a nnn , then the log of other numbers, about this complexity.
Then obviously enumerate nnn can getfn f_nfn, But what you want is mfnm^{f_n}mfn, f n f_n fn You can't take a model, you can't be high-precision.
Then we consider not asking for fn f_nfn, Enum nnn 求出 m f i m^{f_i} mfiThe value of gi g_igi.
Then we can see that the transfer is: gi = gi − 1 a × gi − 2 b g_i=g_{i-1}^a\times g_{i-2}^bgi=gi−1a×gi−2b. (That is, all calculations go to the first level)
In this way, it can be carried out in the sense of modulo without problems.
Code
#include<queue>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define ll long long
#define mo 1000000007
using namespace std;
ll a, b, m, re, tmp, l, r;
int n;
ll ksm(ll x, ll y) {
re = 1ll;
while (y) {
if (y & 1) re = (re * x) % mo;
x = (x * x) % mo;
y >>= 1;
}
return re;
}
int main() {
scanf("%lld %lld %lld %d", &a, &b, &m, &n);
l = r = m;
for (int i = 3; i <= n; i++) {
tmp = (ksm(l, b) % mo * ksm(r, a)) % mo;
l = r;
r = tmp;
}
printf("%lld", r);
return 0;
}