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 ...
随机推荐
- replication-manager 搭建
replication-manager 搭建 介绍 replication-manager 主要用于mysql主从结构的监控和主从切换. 安装 vi /etc/yum.repos.d/signal18 ...
- LINUX修改、增加IP的方法,一张网卡绑定多个IP/漂移IP【转】
临时增加IP命令:ifconfig eth0:1 ip地址 netmask 子网码 broadcast 广播地址 gateway 网关 ifconfig eth0:1 10.1.104.65 net ...
- 使用Python扫描网络MAC地址对应的IP地址
#!/usr/bin/env python # -*- coding: utf-8 -*- from scapy.all import srp,Ether,ARP,conf ipscan='192.1 ...
- Python基础(2):__doc__、文档字符串docString、help()
OS:Windows 10家庭中文版,Python:3.6.4 Python中的 文档字符串(docString) 出现在 模块.函数.类 的第一行,用于对这些程序进行说明.它在执行的时候被忽略,但会 ...
- 数据库-mysql函数
一:MySQL中提供了许多内置函数 CHAR_LENGTH(str) 返回值为字符串str 的长度,长度的单位为字符.一个多字节字符算作一个单字符. 对于一个包含五个二字节字符集, LENGTH()返 ...
- 通过field:global给子元素添加css样式
{dede:arclist row=5 typeid=200} <li [field:global runphp=’yes’ name=autoindex](@me==1)?@me=”class ...
- Centos之文件搜索命令locate
locate命令 locate 文件名 在后台数据库中按文件名搜索,搜索速度更快 /var/lib/mlocate #locate命令所搜索的后台数据库 updatedb 更新数据库 locate搜索 ...
- c语言双向循环链表
双向循环链表,先来说说双向链表,双向链表也叫双链表,是链表的一种,它的每个数据结点中都有两个指针,分别指向直接后继和直接前驱.所以,从双向链表中的任意一个结点开始,都可以很方便地访问它的前驱结点和后继 ...
- 在Kubernetes上使用Traefik
本节内容: Traefik介绍 部署测试用的两个服务 Role Based Access Control configuration (Kubernetes 1.6+ only) 部署Traefik ...
- BZOJ囤题计划
决定做一些题,学习jry,开坑(其实是填坑) 大概会刷的很慢,大家别鄙视我..欢迎鄙视 果然慢出翔了,还是填完吧.. 现在做了: 11 [2338][HNOI2011]数矩形 枚举对角线暴力水过,所有 ...