CodeForces
来源:互联网 发布:网络社交 编辑:程序博客网 时间:2024/06/04 00:23
D. Iterated Linear Function
time limit per test
1 secondmemory limit per test
256 megabytesinput
standard inputoutput
standard outputConsider 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, nand x find the value of g(n)(x) modulo 109 + 7.
Input
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 long integer 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
由于等比数列求和公式需要用到除法,所以取摸运算注意用到逆元。
( a/b ) % c == ( a*b1 ) % c;
b1=pow_mod(b,mod-2,mod);
#include<stdio.h>#include<math.h>#define mod 1000000007long long pow_mod(long long a,long long b,long long mod_val){ long long ans=1; long long t=a%mod_val; while(b) { if(b&1) { ans=ans*t%mod_val; } t = t*t%mod_val; b>>=1; } return ans;}int main(){ long long A,B,n,x,b,ans; while(scanf("%lld%lld%lld%lld",&A,&B,&n,&x)!=EOF) { long long a=pow_mod(A,n,mod); if(A==1) { ans=(x+(n%mod*B))%mod; } else { b=pow_mod(A-1,mod-2,mod); ans=(a-1)%mod; ans=ans*b%mod*B%mod; ans=(ans+a*x+mod)%mod; } printf("%lld\n",ans); }}
阅读全文
0 0
- codeforces~~~
- Codeforces
- codeforces
- Codeforces
- codeforces
- codeforces
- Codeforces
- Codeforces
- CodeForces
- CodeForces
- CodeForces
- CodeForces
- CodeForces
- Codeforces
- Codeforces
- Codeforces
- Codeforces
- Codeforces
- linux内核中的GPIO系统之(2):pin control subsystem
- springboot servlet使用配置
- HTML-父类div高度自适应子类div的高度
- 使用openssl生成RSA公钥和私钥对
- 页面之间参数传递2--Struts标签
- CodeForces
- Eclipse Modeling Tools安装GMF插件
- 栈和队列题
- JQ实现星星评价(带半星)
- mysqldump与innobackupex备份过程你知多少(完结篇)
- sgdisk基本用法
- 总结Unity遇到的坑及优化(持续更新)
- 强制转换的使用技巧
- C语言:指针的运算