1 #include <bits/stdc++.h> 2 using namespace std; 3 4 #define ll long long 5 const int mod=99991; 6 ll head[mod],nex[mod]; 7 ll cnt; 8 ll hs[mod],id[mod]; 9 void init(){ 10 memset(head,-1,sizeof(head)); 11 cnt=0; 12 } 13 void insert_(ll x,ll i){ 14 hs[cnt]=x;id[cnt]=i; 15 ll u=x%mod; 16 nex[cnt]=head[u]; 17 head[u]=cnt++; 18 } 19 ll find_(ll x){ 20 ll u=x%mod; 21 for(ll i=head[u];~i;i=nex[i]){ 22 if(hs[i]==x) return id[i]; 23 } 24 return -1; 25 } 26 ll gcd(ll a,ll b){ 27 ll r=a%b; 28 while(r){a=b;b=r;r=a%b;} 29 return b; 30 } 31 //求解a^x≡b(mod c),返回x,无解返回-1 32 ll BSGS(ll a,ll b,ll c){ 33 a%=c;b%=c; 34 if(b==1) return 0; 35 ll g=1,d=1,k=0; 36 while((g=gcd(a,c))!=1){ 37 if(b%g) return -1; 38 b/=g;c/=g; 39 k++; 40 d=d*(a/g)%c; 41 if(b==d) return k; 42 } 43 ll m=ceil(sqrt(c*1.0)),q=1; 44 for(ll i=0;i<=m;i++){ 45 if(i==0) insert_(b,i); 46 else { 47 q=q*a%c; 48 insert_(q*b%c,i); 49 } 50 } 51 for(ll i=1;i<=m;i++){ 52 d=d*q%c; 53 ll j=find_(d); 54 if(j!=-1) return i*m-j+k; 55 } 56 return -1; 57 } 58 int main(){ 59 ll k,p,n; 60 while(scanf("%lld%lld%lld",&k,&p,&n)!=EOF){ 61 init(); 62 ll ans=-1; 63 if(n<=p) ans=BSGS(k,n,p); 64 if(ans==-1) puts("Orz,I can’t find D!"); 65 else printf("%lld ",ans); 66 } 67 }