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 thelong 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

裸题一道。最近玩SPFA有点过头……矩阵快速幂做了有一段时间了,构造矩阵还想了几分钟,我也是醉了……。好像也可以用普通快速幂,把r=r*bas%mod改成r=(r*bas+b)%mod即可,一开始全部设为int各种苦逼WA……千万记得最后输出的答案再模mod,这里又苦逼WA一发……

代码:

#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<sstream>
#include<cstring>
#include<cstdio>
#include<string>
#include<deque>
#include<stack>
#include<cmath>
#include<queue>
#include<set>
#include<map>
#define INF 0x3f3f3f3f
#define MM(x) memset(x,0,sizeof(x))
#define MMINF(x) memset(x,INF,sizeof(x))
using namespace std;
typedef long long LL;
struct mat
{
LL pos[2][2];
mat(){MM(pos);}
};
LL a,b,n,x;
const int mod=1e9+7;
inline mat operator*(const mat &a,const mat &b)
{
mat c;
for (int i=0; i<2; i++)
{
for (int j=0; j<2; j++)
{
for (int k=0; k<2; k++)
{
c.pos[i][j]+=(a.pos[i][k]*b.pos[k][j])%mod;
}
}
}
return c;
}
inline mat operator^(mat a,LL b)
{
mat bas=a,r;
for (int i=0; i<2; i++)
r.pos[i][i]=1;
while (b!=0)
{
if(b&1)
r=r*bas;
bas=bas*bas;
b>>=1;
}
return r;
}
int main(void)
{
while (cin>>a>>b>>n>>x)
{
mat one,t;
one.pos[0][0]=x;
one.pos[1][0]=1;
t.pos[0][0]=a;
t.pos[0][1]=b;
t.pos[1][1]=1;
t=t^(n);
one=t*one;
cout<<one.pos[0][0]%mod<<endl;
}
return 0;
}

Educational Codeforces Round 13——D. Iterated Linear Function(矩阵快速幂或普通快速幂水题)的更多相关文章

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

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

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

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

  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/contest/678/problem/D D. Iterated Linear Function Consider a linear function f ...

  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

    http://codeforces.com/contest/678 A:水题 #include<bits/stdc++.h> #define fi first #define se sec ...

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

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

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

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

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

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

随机推荐

  1. GetClassName 取得的类名有

    今天上午稍微跟踪了一下自己的项目里面的各个空间,得知GetClassName可以取到以下类名:Static\Edit\Button\ComboBox\msctls_trackbar32\SysTabC ...

  2. Idea注释参数报错,控制台乱码问题解决方法

    idea虽然工具非常好用,但是他的一些解决方法网上非常的少,有些压根没有,解决这些问题非常浪费时间 1.最近在工作中发现一个问题,使用ant打包后,控制台总是报错,提示信息还是乱码的,吓得我赶紧用回了 ...

  3. codevs 2277 爱吃皮蛋的小明(水题日常)

    时间限制: 1 s  空间限制: 32000 KB  题目等级 : 白银 Silver 题目描述 Description 小明特别爱吃蛋,特别是皮蛋.他一次可以吃一个蛋或者两个蛋(整个吞下去),而且他 ...

  4. bzip2命令

    bzip2命令——压缩文件 命令所在路径:/usr/bin/bzip2 示例1: # bzip2 yum.log 压缩当前目录下yum.log文件成yum.log.bz2 示例2: # bzip2 - ...

  5. Vue和SuperSlide做轮播效果

    使用这个插件做轮播需要的js应该知道,就是vue.js和jquery.SuperSlide.2.1.1.js 下载地址: vue:https://vuejs.org/js/vue.js 这里直接Ctr ...

  6. 判断NumLock键和CapsLock键是否被锁定

    实现效果: 知识运用: AIP函数GetKeyState //针对已处理过的按键 在最近一次输入信息时 判断指定虚拟键的状态 intkey:预测试的虚拟键键码 实现代码: [DllImport(&qu ...

  7. 结合浅层高层特征的paper总结

    1.ION:在conv3.conv4.conv5和context features上分别进行roi_pooling,在channel那一维进行concat 2.Hypernet:在较浅层max_poo ...

  8. POI写入word doc 03 模板的实例

    在使用POI写word doc文件的时候我们必须要先有一个doc文件才行,因为我们在写doc文件的时候是通过HWPFDocument来写的,而HWPFDocument是要依附于一个doc文件的.所以通 ...

  9. 清北学堂2018DP&图论精讲班 DP部分学习笔记

    Day 1 上午 讲的挺基础的--不过还是有些地方不太明白 例1 给定一个数n,求将n划分成若干个正整数的方案数. 例2 数字三角形 例7 最长不下降子序列 以上太过于基础,不做深入讨论 例3 给定一 ...

  10. shell脚本,批量创建10个系统帐号并设置密码为随机8位字符串。

    [root@localhost wyb]# cat user10.sh #!/bin/bash #批量创建10个系统帐号wangyb01-wangyb10并设置密码(密码为随机8位字符串). > ...