poj 2417 Discrete Logging（bsgs算法，baby-step giant-step）

Time Limit: 5000MS Memory Limit: 65536K

Given a prime P, 2 <= P < 231, an integer B, 2 <= B < P, and an integer N, 1 <= N < P, compute the discrete logarithm of N, base B, modulo P. That is, find an integer L such that
B^L == N (mod P)

Input

Read several lines of input, each containing P,B,N separated by a space.

Output

For each line print the logarithm on a separate line. If there are several, print the smallest; if there is none, print “no solution”.

Hint

The solution to this problem requires a well known result in number theory that is probably expected of you for Putnam but not ACM competitions. It is Fermat’s theorem that states
B(P-1) == 1 (mod P)

for any prime P and some other (fairly rare) numbers known as base-B pseudoprimes. A rarer subset of the base-B pseudoprimes, known as Carmichael numbers, are pseudoprimes for every base between 2 and P-1. A corollary to Fermat’s theorem is that for any m
B(-m) == B(P-1-m) (mod P) .

Source

Waterloo Local 2002.01.26

题解：

l=i*m-j，m=ceil(√p)，ceil()是向上取整

l=i*m-j=i*ceil(√p)-j<p，所以实际上是要证明l<=p的范围内就能找到合法的l

k mod (p-1)等价于k-m(p-1)，则有b^(k-m(p-1)) ≡ b^k(mod p)，化简得b^(m(p-1)) ≡ 1(mod p)

m=ceil(sqrt(p))可行

代码：

• 版权声明： 本博客所有文章除特别声明外，著作权归作者所有。转载请注明出处！