D. Iterated Linear Function

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, 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.

 
input
3 4 1 1
output
7
input
3 4 2 1
output
25
input
3 4 3 1
output
79
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <iostream>
using namespace std;
const long long MOD = ;
typedef long long ll;
//逆元 费马小定理
ll pow(ll x,ll y)
{
ll res=;
while(y){
if(y&) res=res*x%MOD;
x=x*x%MOD;
y>>=;
}
return res;
} int main()
{
ll a,b,n,x;
cin>>a>>b>>n>>x;
ll res;
if(a==){
res=(x+n%MOD*b)%MOD;
}
else{
res=pow(a,n)*x%MOD;
/*
答案是 pow(A,n) + 一个等比数列
式子可以变成等比数列,然后直接用 Sn = B*a1*(q^n-1)/(q-1) 计算
a1 = 0,q = A
因为每次都要取模,所以要用逆元来算,不能直接除以q-1,而是应该乘以它的乘法逆元
根据费马小定理: 假如MOD是素数,且a与MOD互质,那么a^(MOD-1)=1(mod MOD)
当我们除以 q-1 的时候,我们就是乘以 1/(q-1)
所以 (q-1)^(MOD-1)=1%MOD
两边同乘1/(q-1)得:(q-1)^(MOD-2)=(1-q)%MOD
即是下面的算式
*/
res+=(pow(a,n)-)*pow(a-,MOD-)%MOD*b;
res=(res%MOD+MOD)%MOD;
}
cout<<res<<endl;
return ;
}

2016-06-15



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 逆元+公式+费马小定理

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

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

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

  5. 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 ...

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

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

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

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

  8. Educational Codeforces Round 13 B. The Same Calendar 水题

    B. The Same Calendar 题目连接: http://www.codeforces.com/contest/678/problem/B Description The girl Tayl ...

  9. Educational Codeforces Round 13 A. Johny Likes Numbers 水题

    A. Johny Likes Numbers 题目连接: http://www.codeforces.com/contest/678/problem/A Description Johny likes ...

随机推荐

  1. nginx 414 Request-URI Too Large

    症状 nginx 414 Request-URI Too Large #客户端请求头缓冲区大小,如果请求头总长度大于小于128k,则使用此缓冲区, #请求头总长度大于128k时使用large_clie ...

  2. Java 使用jaxp添加节点

    <?xml version="1.0" encoding="UTF-8"?> <person> <p1> <name& ...

  3. iOS使用代码截图

    /** * 截图代码 * * @param view 需要截图的view * @param rect 需要截取的区域 * * @return 返回截取的对象 */ + (UIImage *)viewS ...

  4. [转]EntityFramework走马观花之CRUD(上)

    学习Entity Framework技术期间查阅的优秀文章,出于以后方便查阅的缘故,转载至Blog,可查阅原文:http://blog.csdn.net/bitfan/article/details/ ...

  5. 03---Net基础加强

    多态---虚方法    (子类可以选择重写或者不重写) class Program { static void Main(string[] args) { Chinese cn1 = new Chin ...

  6. 夺命雷公狗ThinkPHP项目之----企业网站1之快速搭建后台

    我们还是老规矩照老方法,将框架里面多余的东西都干掉,然后在index.php里面将框架搭建起来 <?php //定义项目目录 define('APP_PATH','./WEB/'); //开启调 ...

  7. lower power的IP设计

    在IP的实现过程中,考虑lower power部分进行设计: 1)Partition the design来满足lower power的一些strategies,尤其是power gating和clo ...

  8. 【ruby】安装Ruby

    系统需求 首先确定操作系统环境,不建议在 Windows 上面搞,所以你需要用: Mac OS X 任意 Linux 发行版本 配置系统包 $ sudo apt-get install -y buil ...

  9. update表关联

    第一种: update student set student.age =(select `user`.age from user where id=student.id ) where studen ...

  10. Tomcat内存溢出解决办法

    使用Java程序从数据库中查询大量的数据时出现异常:java.lang.OutOfMemoryError: Java heap space在JVM中如果98%的时间是用于GC且可用的 Heap siz ...