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 ABn 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 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
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include <map>
#include <algorithm>
#include <set>
using namespace std;
#define MM(a,b) memset(a,b,sizeof(a))
#define SC scanf
#define PF printf
#define CT continue
typedef long long ll;
typedef unsigned long long ULL;
const int mod = ;
const double eps = 1e-;
const int inf = 0x3f3f3f3f;
const int N=*1e5+; ll quick(ll a,ll n)
{
ll res=;
while(n){
if(n&) res=(res*a)%mod;
a=(a*a)%mod;
n>>=;
}
return res;
} ll yuan(ll n)
{
return quick(n,mod-);
} int main()
{
ll a,b,n,x;
while(~SC("%lld%lld%lld%lld",&a,&b,&n,&x)){
ll ans=;
ans+=quick(a,n)*x%mod;
ans+=b*(quick(a,n)-)%mod*yuan(a-)%mod;
PF("%lld\n",ans);
}
return ;
}

逆元求法:利用费马小定理

http://blog.csdn.net/qq_21057881/article/details/51758437

最新文章

  1. [8.1] Triple Step
  2. python学习-day16:函数作用域、匿名函数、函数式编程、map、filter、reduce函数、内置函数r
  3. QT编写DLL给外部程序调用,提供VC/C#/C调用示例(含事件)
  4. miniSipServer简单而不简单,轻松落地,实现电脑对固话、手机通讯
  5. 搭建企业cacti服务器
  6. ios 分类(Category)
  7. lua for通过循环table一些差异
  8. Codekart 框架
  9. jQuery版本冲突解决办法
  10. $smary模板缓存
  11. javascript 倒计数功能
  12. python GUI图形化编程-----wxpython
  13. css3-study-new
  14. linux 清空历史命令
  15. linux添加字体
  16. 如何设计一款优秀的短视频 SDK
  17. win7安装mysql-8.0.13-winx64
  18. Session和Cookie,以及用户登录验证问题。
  19. CNN 参数计算
  20. ORACLE表空间操作实例

热门文章

  1. php源码安装执行configure报错error: off_t undefined; check your library configuration
  2. selenium登录4399
  3. 【转载】STM32 IAP 在线升级详解
  4. 什么是Sprint?
  5. 【原创】大叔问题定位分享(36)openresty(nginx+lua)中获取不到post数据,ngx.req.get_body_data返回nil
  6. LeetCode 腾讯精选50题--求众数
  7. 利用浏览器做好数字进制和ASCII码及Unicode教与学
  8. SpringBoot--多环境部署配置文件
  9. 四款免费好用的Bootstrap ui编辑器
  10. 微信小程序开发(十一)获取手机的完整详细信息