BSGS(大步小步算法)学习笔记

发布时间 2023-04-24 19:04:50作者: Forever1507

解决高次同余问题。

\(a^x\equiv b(\mod p)\),其中 \(a\)\(p\) 同余。

这个形式与欧拉定理类似。

思想:meet in the middle(折半搜索)。

具体的,令 \(x=A\times t-B\),且 \(x\) 一定在 \([0,\phi(p))\) 的范围内。但是 \(p\) 是质数时复杂度还是会爆炸。

\(x=A\times t-B\) 带入可得,\(a^{At-B}\equiv b(\mod p)\)

移一下,\(a^{At}\equiv a^Bb(\mod p)\)

\(t=\sqrt p\),那么直接分别枚举 \(A,B\),把每次的 \(A\) 枚举到算出来的值用 map 映射到对应的 \(A\) 上去,再枚举 \(B\) 看看能不能对应即可。