题意:解方程
\[ a^x\equiv x\pmod m \]数据范围:\(a<m\le10^9\)
Solution
首先 \(a^x\equiv a^{x\bmod\varphi(m)+\varphi(m)}\pmod m\)
我们设 \(\text{solve}(\&x,y,m)\) 表示解决
\[a^{x\bmod\varphi(m)+y}\equiv x\pmod m \]原题即 \(\text{solve}(\&x,\varphi(m),m)\)
则 \(\text{solve}(\&x,y,m)\) 相当于
\[\begin{cases}r&\equiv x\pmod{\varphi(m)}\\a^{r+y}&\equiv x\pmod m\end{cases} \]\[x=r+k_1\varphi(m)=a^{r+y}+k_2m \]\[k_1\varphi(m)-k_2m=a^{r+y}-r \]\[k\cdot\gcd(\varphi(m),m)=a^{r+y}-r \]记 \(g=\gcd(\varphi(m),m)\)
\[a^{r+y}-r\equiv0\pmod g \]\[a^{r+y}\equiv r\pmod g \]\[a^{r\bmod\varphi(g)+[y\bmod\varphi(g)+\varphi(g)]}\equiv r\pmod g \]问题变成了 \(\text{solve}(\&r,[y\bmod\varphi(g)+\varphi(g)],g)\)
如果 \(r\) 解决了,那么解出 \(k_1\varphi(m)-k_2m=a^{r+y}-r\),\(x=r+k_1\varphi(m)\)
由于 \(g\) 总是小于 \(m\),问题量总是在缩小
\(m=1\) 时返回 \(x=1\)
省流
\(\text{solve}(y,m)\):
- \(m=1\):返回 \(1\)
- \(m>1\):
- 记 \(g=\gcd(\varphi(m),m)\)
- 调用 \(\text{solve}(y\bmod\varphi(g)+\varphi(g),g)\),记结果为 \(r\)
- 解方程 \(k_1\varphi(m)-k_2m=a^{r+y}-r\)
- 返回 \(r+k_1\varphi(m)\)
答案:\(\text{solve}(\varphi(m),m)\)
标签:pmod,题解,bmod,solve,varphi,text,简单,数学题,equiv From: https://www.cnblogs.com/laijinyi/p/18148385