Educational Codeforces Round 13 D. Iterated Linear Function 水题
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 水题的更多相关文章
- 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 ...
- 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 ...
- Educational Codeforces Round 13 D. Iterated Linear Function (矩阵快速幂)
题目链接:http://codeforces.com/problemset/problem/678/D 简单的矩阵快速幂模版题 矩阵是这样的: #include <bits/stdc++.h&g ...
- Educational Codeforces Round 13 C. Joty and Chocolate 水题
C. Joty and Chocolate 题目连接: http://www.codeforces.com/contest/678/problem/C Description Little Joty ...
- Educational Codeforces Round 13 B. The Same Calendar 水题
B. The Same Calendar 题目连接: http://www.codeforces.com/contest/678/problem/B Description The girl Tayl ...
- Educational Codeforces Round 13 A. Johny Likes Numbers 水题
A. Johny Likes Numbers 题目连接: http://www.codeforces.com/contest/678/problem/A Description Johny likes ...
- codeforces 678D D. Iterated Linear Function(水题)
题目链接: D. Iterated Linear Function time limit per test 1 second memory limit per test 256 megabytes i ...
- Educational Codeforces Round 14 A. Fashion in Berland 水题
A. Fashion in Berland 题目连接: http://www.codeforces.com/contest/691/problem/A Description According to ...
- Educational Codeforces Round 4 A. The Text Splitting 水题
A. The Text Splitting 题目连接: http://www.codeforces.com/contest/612/problem/A Description You are give ...
随机推荐
- container_of分析【转】
转自:http://blog.csdn.net/tigerjibo/article/details/8299589 1.container_of宏 1> Container_of在Linux内核 ...
- C# 去除文件非法字符名
string resultFileName = MD5Encrypt(NavigateUrl).Replace("=",string.Empty) + ".txt&quo ...
- ARC073E Ball Coloring
Problem AtCoder Solution 把点映射至二维平面,问题就变成了给定 \(n\) 个点,可以把点对 \(y=x\) 对称,求覆盖所有点的最小矩形面积. 可以先把所有点放到 \(y=x ...
- Python3 item系列
一.前言 #在python中一切皆对象 ''' 创建了一个dict实例-->dic就是dict的实例对象 我们通过dic['k1']可以得到k1所对应的值 那么我们自定义一个类,可不可以使用对象 ...
- MySQL权限操作:Grant、Revoke
数据库操作: 创建数据库.创建表——CREATE 删除数据库.删除表——DROP 删除表内容——TRUNCATE.DELETE(后者效率低.一行一行地删除记录) 查询数据库.查询表——SELECT 插 ...
- 最后一面《HR面》------十大经典提问
1.HR:你希望通过这份工作获得什么? 1).自杀式回答:我希望自己为之工作的企业能够重视质量,而且会给做得好的员工予以奖励.我希望通过这份工作锻炼自己,提升自己的能力,能让公司更加重视我. a.“我 ...
- delphi TComponent类(1)
来自:http://blog.csdn.net/lailai186/article/details/7442383 ------------------------------------------ ...
- HTML5元素2
用于分组的元素 元素 说明 类型 HTML5与其他的变化 blockquote 表示引自他处的大段内容 流 无变化 dd 用在dl元素之中,表示定义 无 无变化 div 一个没有任何既定语义的通用元素 ...
- Linux 下用 smartd 监测硬盘状况
https://blog.csdn.net/hanxuehen/article/details/6024826
- Java开发工程师学习路线
贴一个比较出名的Java开发工程师学习路线图 好好学习提升中 这个貌似也不是特别全,算法,设计模式,架构好像都没有