http://acm.hdu.edu.cn/showproblem.php?pid=2815
Test instructions: Bare title ...
Solution: There is another pit, is the n>=p when the output is not solved.
1#include <cstdio>2#include <cstdlib>3#include <cstring>4#include <iostream>5#include <cmath>6#include <algorithm>7 using namespacestd;8 9typedefLong LongLL;Ten Const intSize=40000; One LL k,p,n; A intBL; - structnode{ - LL D,id; the }bit[size]; - - BOOLCMP (node X,node y) { - if(X.D==Y.D)returnx.id<y.id; + returnx.d<Y.d; - } + All EXGCD (ll a,ll b,ll &x,ll &y) at { - if(b==0) {x=1, y=0;returnA;} - LL tx,ty; -LL D=EXGCD (b,a%b,tx,ty); -X=ty;y=tx-(A/b) *Ty; - returnD; in } - to ll find (ll x) + { - intL=0, r=BL; the while(l<=R) * { $ intMid= (l+r) >>1;Panax Notoginseng if(bit[mid].d==x)returnbit[mid].id; - if(bit[mid].d<x) l=mid+1; the if(bit[mid].d>x) r=mid-1; + } A return-1; the } + - LL exbsgs () $ { $ if(n>=p)return-1; - LL t,m,a,b,c,g,k,x,y,add,pm,am; -k=1; a=k;b=n;c=p;add=0; t=1; the for(intI=0; i<= -; i++)//before the numerator - {Wuyi if(t%c==b)returni; thet=t*a%C; - } Wu while((G=EXGCD (k,c,x,y))! =1) - { Aboutk= (k*k/g)%C; $c/=g;//not mod . - if(b%g)return-1; -b/=g;add++; - } AM= (LL) (Ceil (Double) sqrt (Double) (c) )); +pm=1%c;bit[0].d=k%c;bit[0].id=0; the for(intI=1; i<=m;i++) - { $bit[i].d=bit[i-1].d*a%C; theBit[i].id=i; thepm=pm*a%C; the } theSort (bit,bit+1+m,cmp); -Bl=0; in for(intI=1; i<=m;i++) the { the if(BIT[I].D!=BIT[BL].D) bit[++bl]=Bit[i]; About } the EXGCD (pm,c,x,y); theAm=x%c+c;//x thet=b%C; + for(intI=0; i<=m;i++) - { thex=find (t);Bayi if(x!=-1)returni*m+x+add; thet=t*am%C; the } - return-1; - } the the intMain () the { the //freopen ("a.in", "R", stdin); - //freopen ("A.out", "w", stdout); the while(SCANF ("%i64d%i64d%i64d", &k,&p,&n)! =EOF) the { theLL ans=exbsgs ();94 if(ans==-1) printf ("Orz,i can ' t find d!\n"); the Elseprintf"%i64d\n", ans); the } the return 0;98}hdu2815
"Hdu2815-mod Tree" higher-secondary congruence equation-expanding badystepgaintstep