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

Educational Codeforces Round 13 D. Iterated Linear Function 逆元+公式+费马小定理的更多相关文章

  1. Educational Codeforces Round 13 D. Iterated Linear Function 水题

    D. Iterated Linear Function 题目连接: http://www.codeforces.com/contest/678/problem/D Description Consid ...

  2. Educational Codeforces Round 13——D. Iterated Linear Function(矩阵快速幂或普通快速幂水题)

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

  3. Educational Codeforces Round 13 D. Iterated Linear Function (矩阵快速幂)

    题目链接:http://codeforces.com/problemset/problem/678/D 简单的矩阵快速幂模版题 矩阵是这样的: #include <bits/stdc++.h&g ...

  4. Educational Codeforces Round 13 D:Iterated Linear Function(数论)

    http://codeforces.com/contest/678/problem/D D. Iterated Linear Function Consider a linear function f ...

  5. CodeForces 300C Beautiful Numbers(乘法逆元/费马小定理+组合数公式+高速幂)

    C. Beautiful Numbers time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  6. codeforces div2_604 E. Beautiful Mirrors(期望+费马小定理)

    题目链接:https://codeforces.com/contest/1265/problem/E 题意:有n面镜子,你现从第一面镜子开始询问,每次问镜子"今天我是否美丽",每天 ...

  7. Educational Codeforces Round 13 A、B、C、D

    A. Johny Likes Numbers time limit per test 0.5 seconds memory limit per test 256 megabytes input sta ...

  8. Educational Codeforces Round 13 E. Another Sith Tournament 状压dp

    E. Another Sith Tournament 题目连接: http://www.codeforces.com/contest/678/problem/E Description The rul ...

  9. Educational Codeforces Round 13 C. Joty and Chocolate 水题

    C. Joty and Chocolate 题目连接: http://www.codeforces.com/contest/678/problem/C Description Little Joty ...

随机推荐

  1. DAG添边定理

    让DAG变成强连通就是把尾和头连起来,也就是入度和出度为0的点,添的边数:max(num_in==0,num_out==0)

  2. 笔记-2:python基本数据类型

    1.数字类型 1.1 整数类型 整数类型有4种进制表示:十进制,二进制,八进制,十六进制,默认情况下,整数采用十进制. 整数类型有4种进制:十进制. 二进制. 八进制和十六进制. 默认情况, 整数采用 ...

  3. JS基础_立即执行函数

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  4. JAVA中ArrayList重写比较方法

    ArrayList的sort方法重写: 当ArrayList中存放的为含有多个成员变量的数据类型时,在进行sort排序时要重写比较方法,一般都写在这个数据类型类的内部如: package 算法竞赛入门 ...

  5. LeetCode 腾讯精选50题--子集

    根据题意,找到几何中的所有子集,说实话子集是没有什么头绪的,因为如果采用遍历的方法,稍有遗漏不说,代码的嵌套循环层数随着数组大小的增加而增加,想了很久没有头绪后就去看了看评论,然后就被点破了解题的关键 ...

  6. git 报错fatal: not a git repository (or any of the parent directories): .git

    产生原因:一般是没有初始化git本地版本管理仓库,所以无法执行git命令 解决方法:操作之前执行以下命令行:  git init 初始化git,即可解决:

  7. 轮播图--使用原生js的轮播图

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  8. javascript字符串机油

    1.创建字符串和数组的方法 1.1创建字符串的方法 a.直接数量:var str=“: b.字符串对象创建:新字符串(“): 1.2创建阵列的方法 a.var.arr=要素…. b.var arr=n ...

  9. Navicat for MySQL 设置定时任务(事件)

    1.查询界面输入命令,查看定时任务是否开启,未开始时OFF: show variables like '%event_scheduler%'; 2. 查询界面输入命令,开启定时任务: set glob ...

  10. Python之IDE工具下载安装及注册详解及创建项目

    这篇文章很适合刚接触python语言的或者没有语言基础的同学参考: 目录: 一.IDE工具下载安装 二.IDE注册方法 三.使用IDE 开发工具使用创建项目 一.下载并安装, IntelliJ IDE ...