$f(x) = Ax+B$
$g^{(0)}(x) = x, g^{(n)}(x) = f(g^{(n-1)}(x))$
给出$A,B,n,x$求$g^{(n)}(x)\pmod {10^9+7}$
">Iterated Linear Function
Iterated Linear Function
Created by LXC on Fri Jul 7 08:55:46 2023
https://codeforces.com/problemset/problem/678/D
ranting: 1700
tag: math, number theory
problem
$f(x) = Ax+B$
$g^{(0)}(x) = x, g^{(n)}(x) = f(g^{(n-1)}(x))$
给出$A,B,n,x$求$g^{(n)}(x)\pmod {10^9+7}$
solution
$g^{(0)}(x) = x$
$g^{(1)}(x) = Ax+B$
$g^{(2)}(x) = A(Ax+B)+B = A^2x+AB+B$
$g^{(3)}(x) = A(A(Ax+B)+B)+B = A^3x+A^2B+AB+B$
$\cdots$
$g^{(n)}(x) = A^nx+(A^{n-1}+A^{n-2}+\cdots+A^0)B=A^nx+\frac{A^n-1}{A-1}B$
需要用到快速幂,以及乘法逆元。乘法逆元可以基于费马小定理用快速幂求出。
当A-1为0是特殊判断,否则求A-1在模1e9+7下的逆元。
code
1 |
|