A x ≡ B ( m o d C ) , g c d ( A , C ) = 1 A^x\equiv B(mod~C),gcd(A,C)=1 Ax≡B(mod C),gcd(A,C)=1
t = C , x = i ∗ t − j , A i t − j ≡ B ( m o d C ) t=\sqrt{C},x=i*t-j,A^{it-j}\equiv B(mod~C) t=C ,x=i∗t−j,Ait−j≡B(mod C)
A i t ≡ A j ∗ B ( m o d C ) A^{it}\equiv A^j*B(mod~C) Ait≡Aj∗B(mod C)
d = g c d ( A , C ) d=gcd(A,C) d=gcd(A,C)
A x − 1 ∗ A d ≡ B d ( m o d C d ) A^{x-1}*\frac{A}{d}\equiv \frac{B}{d}(mod~\frac{C}{d}) Ax−1∗dA≡dB(mod dC)
A x − c n t ∗ A c n t ∗ Π d ≡ B Π d ( m o d C Π d ) A^{x-cnt}*A^{cnt}*\Pi_d\equiv \frac{B}{\Pi_d}(mod~\frac{C}{\Pi_d}) Ax−cnt∗Acnt∗Πd≡ΠdB(mod ΠdC)
#include
#include
using namespace std;
int a,p,b,t,cnt;
unordered_map<int,int>mp;
int gcd(int a,int b){
if(!b) return a;
return gcd(b,a%b);
}
int mul(int a,int b,int p){return 1LL*a*b%p;}
int exbsgs(int a,int b,int p){
//if(!a&&b) return -1;
if(b==1) return 0;
cnt=0;int d,k=1;
for(d=gcd(a,p);d!=1;d=gcd(a,p)){
if(b%d) return -1;
b/=d,p/=d,cnt++,k=mul(k,a/d,p);
if(k==b) return cnt;
}
t=sqrt(p)+1;int kt=1;
mp.clear();
for(int i=0;i<t;++i){
mp[mul(kt,b,p)]=i;
kt=mul(kt,a,p);
}
k=mul(k,kt,p);
for(int i=1;i<=t;++i){
if(mp.find(k)!=mp.end()) return i*t-mp[k]+cnt;
k=mul(k,kt,p);
}
return -1;
}
int main(){
while(scanf("%d%d%d",&a,&p,&b)&&a&&p&&b){
int ans=exbsgs(a,b,p);
if(~ans) printf("%d\n",ans);
else printf("No Solution\n");
}
return 0;
}