D. Iterated Linear Function

题目连接:

http://www.codeforces.com/contest/678/problem/D

Description

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

Sample Input

3 4 1 1

Sample Output

7

Hint

题意

g(0)(x) = x

g(n)(x)=f(g(n-1)(x))

f(x) = ax+b

给你a b n x

求g(n)(x)

题解:

推一下 发现 g(n)(x) = anx+a(n-1)b+a^(n-2)b+...+ab+b

其实就是等比数列……

快速幂直接莽一波就好了

特判 a=1的时候

代码

#include<bits/stdc++.h>
using namespace std;
const long long mod = 1e9+7;
long long quickpow(long long m,long long n,long long k)//返回m^n%k
{
long long b = 1;
while (n > 0)
{
if (n & 1)
b = (b*m)%k;
n = n >> 1 ;
m = (m*m)%k;
}
return b;
} int main()
{
long long a,b,n,x;
cin>>a>>b>>n>>x;
if(a==1)
{
cout<<(n%mod*b+x)%mod<<endl;
return 0;
}
long long 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;
printf("%lld\n",ans%mod);
}

Educational Codeforces Round 13 D. Iterated Linear Function 水题的更多相关文章

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

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

  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 C. Joty and Chocolate 水题

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

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

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

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

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

  7. codeforces 678D D. Iterated Linear Function(水题)

    题目链接: D. Iterated Linear Function time limit per test 1 second memory limit per test 256 megabytes i ...

  8. Educational Codeforces Round 14 A. Fashion in Berland 水题

    A. Fashion in Berland 题目连接: http://www.codeforces.com/contest/691/problem/A Description According to ...

  9. Educational Codeforces Round 4 A. The Text Splitting 水题

    A. The Text Splitting 题目连接: http://www.codeforces.com/contest/612/problem/A Description You are give ...

随机推荐

  1. 002_IO磁盘深入理解

    一.如何测试云硬盘 https://www.ustack.com/blog/how-benchmark-ebs/#fio

  2. HTML5学习--SVG全攻略(基础篇)

    明天高级篇 一.什么是SVG? SVG 指的是可伸缩矢量图形 (Scalable Vector Graphics),它用来定义用于网络的基于矢量的图形,使用 XML 格式定义图形.SVG 图像在放大或 ...

  3. js实现table导出Excel,保留table样式

    浏览器环境:谷歌浏览器 1.在导出Excel的时候,保存table的样式,有2种方法,①是在table的行内写style样式,②是在模板里面添加样式 2.第一种方式:行内添加样式 <td sty ...

  4. Ubuntu下使用Nginx+uWSGI+Flask(初体验)

    Ubuntu 18.04,Nginx 1.14.0, uWSGI 2.0.17.1,Flask, 前言 Windows不支持uWSGI!为了上线自己的项目,只能选择Linux. 自己前面开发了一个Fl ...

  5. SQLite数据库初步

    Windows 10家庭中文版 想使用Python操作SQLite数据库,可是,不知道怎么建立数据库文件. 在SQLite官网溜达了一圈,总算使用上面的工具安装了建立了我需要的数据库文件. 1.进入官 ...

  6. ZooKeeper常见问题

    转载自原文:zookeeper(二)常见问题汇总 一.为什么zookeeper要部署基数台服务器? 所谓的zookeeper容错是指,当宕掉几个zookeeper服务器之后,剩下的个数必须大于宕掉的个 ...

  7. 20165203《Java程序设计》第二周Java学习总结

    教材学习内容总结 第二章 (一)标识符 注意: 标识符由字母.下画线.美元符号和数字组成,长度不受限制. 标识符第一个字符不能是数学字符. 标识符不能是关键字. 标识符不能是true.false和nu ...

  8. 如何用python解析mysqldump文件

    一.前言 最近在做离线数据导入HBase项目,涉及将存储在Mysql中的历史数据通过bulkload的方式导入HBase.由于源数据已经不在DB中,而是以文件形式存储在机器磁盘,此文件是mysqldu ...

  9. WordPress前台后台出现一片空白的原因以及解决办法

    WordPress前台后台出现空白的可能原因有以下: 这个问题,一般是在进行以下操作后出现的: 1.网站更换新主题2.网站安装或升级插件3.升级了Wordpress版本 其实问题的根源在于你的主题.插 ...

  10. JS几种变量交换方式以及性能分析对比

    前言 "两个变量之间的值得交换",这是一个经典的话题,现在也有了很多的成熟解决方案,本文主要是列举几种常用的方案,进行大量计算并分析对比. 起由 最近做某个项目时,其中有一个需求是 ...