Test instructions
Give P,b,n the smallest l make b^l==n (mod p).
Test instructions
Equivalent in the 0~P-1 search L satisfy the same, baby_step,giant_step equivalent to the sub-block of the binary search, set M=SQRT (p), the p is divided into M*m, in M block per block of m number of binary search.
Code:
POJ 2417//sep9#include <iostream> #include <cmath> #include <algorithm>using namespace Std;const int maxn=100000;typedef Long Long ll;struct node{int Ids;ll v;} Baby[maxn];int cmp (Node X,node y) {if (X.V!=Y.V) return X.v<y.v;return X.ids<y.ids;} ll Pow_mod (ll a,ll b,ll MoD) {ll ans=1;while (b) {if (b&1) ans= (ans*a)%mod;a= (a*a)%mod;b>>=1;} return ans;} int bsearch (int len,ll v) {int L=0,r=len,mid;while (l<r) {mid= (l+r)/2;if (baby[mid].v==v) return Baby[mid].ids;else if (Baby[mid].v<v) L=mid+1;elser=mid;} return-1;} int main () {ll p,b,n;while (scanf ("%i64d%i64d%i64d", &p,&b,&n) ==3) {int m= (int) ceil (sqrt ((p-1) *1.0)); baby [0].ids=0,baby[0].v=1;for (int i=1;i<m;++i) {baby[i].ids=i;baby[i].v= (baby[i-1].v*b)%p;} Sort (baby,baby+m,cmp); int cnt=1;for (int i=1;i<m;++i) if (BABY[I].V!=BABY[I-1].V) Baby[cnt++]=baby[i];ll Bm=pow_ MoD (Pow_mod (b,p-2,p), m,p), int ans=-1;for (int i=0;i<m;++i) {int J=bsearch (cnt,n), if (j!=-1) {ans=i*m+j;break;} N= (N*BM)%p;} if (ans<0) puts ("No solution "), Else printf ("%d\n ", ans); return 0;}
POJ 2417 Discrete Logging number theory Baby_step,giant_step algorithm