g(n)(x) = (A^n*x + (A^(n-1) + A^(n-2) + A^(n-3) + ...A) * B) % 1000000007
这个式子用快速幂,等比数列求和公式,费马小定理求乘法逆元,就可以解决
#include <bits/stdc++.h>
#define INF 1e9
using namespace std;
typedef long long ll;
ll solve(ll d, ll n, ll MOD){
ll s = 1;
while(n){
if(n&1)
s = s * d % MOD;
n /= 2;
d = d * d % MOD;
}
return s;
}
int main(){
// freopen("in.txt", "r", stdin);
ll a, b, n, x, MOD = 1000000007;
cin >> a >> b >> n >> x;
if(a == 1){
cout << (x + n % MOD * b) % MOD << endl;
}
else{
ll sum;
sum = solve(a, n, MOD);
sum = sum * x % MOD;
ll p1 = solve(a-1, MOD-2, MOD);
ll p2 = (solve(a, n, MOD) - 1 + MOD) % MOD;
sum = (sum + p1 * p2 % MOD * b% MOD) % MOD;
cout << sum << endl;
}
return 0;
}
Consider a linear function f(x) = Ax + B. Let's define g(0)(x) = x and g(n)(x) = f(g(n - 1)(x)) for n > 0. For the given integer values A, B,n and x find the value of g(n)(x) modulo 109 + 7.
The only line contains four integers A, B, n and x (1 ≤ A, B, x ≤ 109, 1 ≤ n ≤ 1018) — the parameters from the problem statement.
Note that the given value n can be too large, so you should use 64-bit integer type to store it. In C++ you can use the long longinteger type and in Java you can use long integer type.
Print the only integer s — the value g(n)(x) modulo 109 + 7.
3 4 1 1
7
3 4 2 1
25
3 4 3 1
79