Educational Codeforces Round 13 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.
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.
Print the only integer s — the value g(n)(x) modulo 109 + 7.
3 4 1 1
7
3 4 2 1
25
3 4 3 1
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(数论)的更多相关文章
- Educational Codeforces Round 13 D. Iterated Linear Function 水题
D. Iterated Linear Function 题目连接: http://www.codeforces.com/contest/678/problem/D Description Consid ...
- 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 A、B、C、D
A. Johny Likes Numbers time limit per test 0.5 seconds memory limit per test 256 megabytes input sta ...
- Educational Codeforces Round 13 E. Another Sith Tournament 状压dp
E. Another Sith Tournament 题目连接: http://www.codeforces.com/contest/678/problem/E Description The rul ...
- 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 ...
随机推荐
- 初涉MySQL
本文要点: MySQL基础 安装MySQL 配置MySQL 使用MYSQL 一.MySQL基础 MySQL 由瑞典MySQL AB公司开发,目前属于Oracle公司. MySQL是一个开源的关系型数据 ...
- 第十一篇 SQL Server安全审核
本篇文章是SQL Server安全系列的第十一篇,详细内容请参考原文. SQL Server审核SQL Server审核是指你可以在数据库或服务器实例监控事件.审核日志包含你选择捕获的事件的列表,在服 ...
- VC++ 修改资源页面语言
选中 资源文件,在“属性”页面中更改 “language”选项: 资源视图中: 属性 页面中:
- Perl Debug error: SetConsoleMode failed, LastError=|6|
Windows Strawberry Perl. 解决办法: 1. 设置环境变量 TERM = dumb 2. 重启 CMD 参考资料: http://padre.perlide.org/trac/t ...
- 使用ajax请求,模态框调用并更改密码
前端页面 <a href="javascript:void(0);" onclick="changPassword()"> <i class= ...
- 发现前端框架 bui-min.js
http://www.builive.com/apps/default/main.html#menu/code http://www.builive.com/demo/grid-plugin.php# ...
- 错过C++
曾相识的C++--2008年那是还在学校,接触到了这门语言,可遗憾的是当时,自己没有能静下心好好学习这门语言.所以相识了了半年的c++就这样不见. 如今又相逢,但已经感觉到很陌生,陌生的我们互补相认.
- DNS域名解析
之前用的是DNSPOD.CN的解析,没什么问题. 因为想根据不同国家做服务器解析(欧洲,亚洲,美国) 然后就转到DNSPOD.com里面有按国家来分的. 但是欧洲那边反应,每天断断续续会不能访问网站. ...
- mysql 查看是否存在某一张表
判断表是否存在 SELECT table_name FROM information_schema.TABLES WHERE table_name ='yourname'; 或者 SHOW TABLE ...
- C++Primer 第四章
//1.当我们对运算符进行重载的时候,其包括运算对象的类型和返回值的类型都是由该运算符定义的,但是运算对象的个数和优先级,结合律都是不能改变的 //2.当一个对象被用作右值的时候,用的是对象的值(内容 ...