Title: Given P,b,n, find the smallest L make b^l≡n (mod p) (p is prime)
Naked bsgs ... Practice practiced hand--
#include <cmath> #include <cstdio> #include <cstring> #include <iostream> #include < algorithm> #define M 100100#define INF 0x3f3f3f3fusing namespace std;typedef pair<long long,long long> Abcd;long Long a,b,c;namespace hash_set{struct list{list *next;int key,val; List (list *_,int __): Next (_), key (__), Val (INF) {}}*head[m];int tim[m],t;inline void Clear () {++t;} int& Hash (int key) {int x=key%m;if (tim[x]!=t) tim[x]=t,head[x]=0x0; List *temp;for (Temp=head[x];temp;temp=temp->next) if (temp->key==key) return temp->val;head[x]=new list ( Head[x],key); return head[x]->val;}} ABCD EXGCD (Long long X,long long y) {if (!y) return ABCD (1,0); ABCD TEMP=EXGCD (y,x%y); return ABCD (temp.second,temp.first-x /y*temp.second);} int Exbsgs () {using namespace Hash_set;int I;long long a_m,d,m= (int) ceil (sqrt (C) +1e-7); Clear (); for (i=0,a_m=1;i<m;i++, (a_m*=a)%=c) {int &temp=hash (a_m); Temp=min (temp,i);} For (i=0,d=1;i<=m;i++, (d*=a_m)%=c) {ABCD temp=exgcd (d,c); Long Long x= (temp.first*b%C+c)%c;if (Hash (x)!=inf) return I*m+hash (x);} return-1;} int main () {while (cin>>c>>a>>b) {int ans=exbsgs (), if (!~ans) puts ("no solution"), Else printf ("%d\n", ans);} return 0;}
Bzoj 3239 Discrete Logging baby-step-giant-step