Educational Codeforces Round 13 Iterated Linear Function(数学)

来源:互联网 发布:淘宝开店怎么上传宝贝 编辑:程序博客网 时间:2024/04/28 20:47

思路:推一下公式可以知道其实就是a^n*x+a^(n-1)*b+a^(n-2)*b+....b,就是一个等比数列求和嘛,注意特判1,可是因为范围太大,所以就要用到模乘法以及逆元。

           a^(mod) % mod = a

           a^(mod-1) % mod = 1

           a^(mod-2) * a %mod = 1

           所以a^(mod-2)是a的逆元,这样就可以将等比数列求和里面的除给变成乘法了


#include<bits\stdc++.h>using namespace std;#define LL long longconst LL mod = 1e9+7;LL quickpow(LL m,LL n,LL k){     LL b = 1; while(n>0) { if(n&1) b = (b*m)%k; n>>=1; m = (m*m)%k; } return b;}int main(){     LL a,b,n,x; cin >> a >> b >> n >> x; if(a==1) {         cout << (x+(n%mod*b))%mod << endl; } else {        LL ans = (quickpow(a,n,mod)-1+mod)%mod;        ans = ans *quickpow(a-1,mod-2,mod)%mod*b%mod;ans = (ans+quickpow(a,n,mod)*x+mod)%mod;cout << ans << endl; }}


D. Iterated Linear Function
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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 AB,n and x find the value of g(n)(x) modulo 109 + 7.

Input

The only line contains four integers ABn 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.

Output

Print the only integer s — the value g(n)(x) modulo 109 + 7.

Examples
input
3 4 1 1
output
7
input
3 4 2 1
output
25
input
3 4 3 1
output
79


0 0
原创粉丝点击