OSDN Git Service

增加递归求解逆元的操作
authorJellyGoat <48863570+JellyGoat@users.noreply.github.com>
Mon, 29 Jul 2019 01:10:31 +0000 (09:10 +0800)
committerGitHub <noreply@github.com>
Mon, 29 Jul 2019 01:10:31 +0000 (09:10 +0800)
话说增加了记忆化之后复杂度是多少QAQ

docs/math/inverse.md

index ae57a33..edb14a0 100644 (file)
@@ -81,6 +81,12 @@ for (int i = 2; i <= n; ++i) inv[i] = (long long)(p - p / i) * inv[p % i] % p;
 
 这就是线性求逆元。
 
+另外,根据线性求逆元方法的式子:$i^{-1} \equiv -kj^{-1}+ \pmod p$  
+
+递归求解$j^-1$,直到$j=1$返回1。
+
+中间优化可以加入一个记忆化来避免多次递归导致的重复。
+
 ### 线性求任意 n 个数的逆元
 
 上面的方法只能求 $1$ 到 $n$ 的逆元,如果需要求任意给定 $n$ 个数( $1 \le a_i < p$ )的逆元,就需要下面的方法: