1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
| #include<iostream> #include<cstdio> #include<cmath> #include<map> #define ll long long using namespace std; namespace Ehnaev{ inline ll read() { ll ret=0,f=1;char ch=getchar(); while(ch<48||ch>57) {if(ch==45) f=-f;ch=getchar();} while(ch>=48&&ch<=57) {ret=(ret<<3)+(ret<<1)+ch-48;ch=getchar();} return ret*f; } inline void write(ll x) { static char buf[22];static ll len=-1; if(x>=0) {do{buf[++len]=x%10+48;x/=10;}while(x);} else {putchar(45);do{buf[++len]=-(x%10)+48;x/=10;}while(x);} while(len>=0) putchar(buf[len--]); } }using Ehnaev::read;using Ehnaev::write; inline void writeln(ll x) {write(x);putchar(10);}
const ll N=1e6;
ll a,p,b,pp,bb,tot,k,sp,ans,flg; ll val[N+5],nxt[N+5]; map<ll,ll> head;
inline ll Exgcd(ll a,ll b,ll &x,ll &y) { if(b==0) {x=1;y=0;return a;}ll r=Exgcd(b,a%b,y,x);y-=a/b*x;return r; }
inline ll Pow(ll b,ll p,ll mo) { ll r=1;while(p) {if(p&1) r=r*b%mo;b=b*b%mo;p>>=1;}return r; }
inline void Addhash(ll x,ll v) { val[++tot]=v; if(head.find(x)!=head.end()) nxt[tot]=head[x]; head[x]=tot; }
inline void Findhash(ll x,ll v) { if(head.find(x)!=head.end()) { for(ll i=head[x];i;i=nxt[i]) { ll tmp=v*sp-val[i]; if(tmp>=0) { if(ans<0||tmp<ans) ans=tmp; } } } }
inline ll BSGS(ll a,ll b,ll p) { for(ll i=1;i<=tot;i++) nxt[i]=val[i]=0; tot=0;head.clear(); sp=ceil(sqrt(p)); ll r=b%p;for(ll i=0;i<=sp;i++) {Addhash(r,i);r=r*a%p;} ll sr=Pow(a,sp,p);r=1;ans=-k-1; for(ll i=0;i<=sp;i++) {Findhash(r,i);r=r*sr%p;} return ans; }
inline void Init() { ll d=1,tmpp=1;k=0;ll x0=0,y0=0; while(Exgcd(a,p/d,x0,y0)!=1) { k++; ll tmpd=Exgcd(a,p/d,x0,y0); d=d*tmpd; if(b%d!=0) {flg=0;break;} } pp=p/d; if(flg) { tmpp=Pow(a,k,pp); Exgcd(tmpp,pp,x0,y0); x0=(x0%pp+pp)%pp;bb=b*x0%pp; } }
int main() {
while(1) { a=read();p=read();b=read(); if(a==0&&p==0&&b==0) break;flg=1;b%=p; if(b==1||p==1) {writeln(0);continue;} Init(); if(!flg) { ll x=-1,r=1; for(ll i=0;i<=k;i++) { if(r==b) {x=i;break;}r=r*a%p; } if(x>=0) writeln(x); else printf("No Solution\n"); } else { ll x=BSGS(a,bb,pp)+k; ll r=1; for(ll i=0;i<=k;i++) { if(r==b) {x=i;break;}r=r*a%p; } if(x<0) {printf("No Solution\n");} else writeln(x); } }
return 0; }
|