POJ 2417 Discrete Logging(离散对数-小步大步算法)

时间:2023-12-05 14:09:20

Description

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".
题目大意:求离散对数,B^L = N (mod P),其中P是素数,求L。
思路:大步小步算法的模板题。
PS:白书上的模板少了个ceil向上取整……
PS:用map来哈希TLE了。速度和手写哈希原来差距这么大……
代码(47MS):
 #include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
using namespace std;
typedef long long LL; const int SIZEH = ; struct hash_map {
int head[SIZEH], size;
int next[SIZEH];
LL state[SIZEH], val[SIZEH]; void init() {
memset(head, -, sizeof(head));
size = ;
} void insert(LL st, LL sv) {
LL h = st % SIZEH;
for(int p = head[h]; ~p; p = next[p])
if(state[p] == st) return ;
state[size] = st; val[size] = sv;
next[size] = head[h]; head[h] = size++;
} LL find(LL st) {
LL h = st % SIZEH;
for(int p = head[h]; ~p; p = next[p])
if(state[p] == st) return val[p];
return -;
}
} hashmap; LL inv(LL a, LL n) {
if(a == ) return ;
return ((n - n / a) * inv(n % a, n)) % n;
} LL pow_mod(LL x, LL p, LL n) {
LL ret = ;
while(p) {
if(p & ) ret = (ret * x) % n;
x = (x * x) % n;
p >>= ;
}
return ret;
} LL BabyStep(LL a, LL b, LL n) {
LL e = , m = LL(ceil(sqrt(n + 0.5)));
hashmap.init();
hashmap.insert(, );
for(int i = ; i < m; ++i) {
e = (e * a) % n;
hashmap.insert(e, i);
}
LL v = inv(pow_mod(a, m, n), n);
for(int i = ; i < m; ++i) {
LL t = hashmap.find(b);
if(t != -) return i * m + t;
b = (b * v) % n;
}
return -;
} int main() {
LL p, b, n;
while(cin>>p>>b>>n) {
LL ans = BabyStep(b, n, p);
if(ans == -) puts("no solution");
else cout<<ans<<endl;
}
}