Wrong Answer.wrong answer On line 260 column 1, read N, expected 1.
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<map>
#include<unordered_map>
using namespace std;
#define re register
#define int long long
快读略
void exgcd(int a,int b,int &x,int &y)
{
if (b==0)
{
x=1;y=0;return;
}
else
{
exgcd(b,a%b,x,y);
int t=x;x=y;y=t-a/b*y;
return;
}
}
int a,b,p;
int qp(int d,int up)
{
if (up==0) return 1;
if (up==1) return d;
int t=1;
t=qp(d,up>>1);
t=t*t%p;
if (up&1) t=t*d%p;
return t;
}
int inv(int a)
{
int x,y;
exgcd(a,p,x,y);
return (x%p+p)%p;
}
map<int,int>ma;
int bsgs(int a,int b,int p)
{
ma.clear() ;
int sq=ceil(sqrt(p));
for (int B=1;B<=sq;++B)
{
b*=a;b%=p;ma[b]=B;
}
int lft=1;
int beg=qp(a,sq)%p;
for (int A=1;A<=sq;++A)
{
lft*=beg;lft%=p;
//if (ma[lft])
if (ma.find(lft) !=ma.end() )
{
return A*sq-ma[lft];
}
}
return -1;
}
signed main()
{
//freopen(".in","r",stdin);
//freopen(".out","w",stdout);
while (1)
{
a=read();p=read();b=read();
if (a==0&&p==0&&b==0) break;
a=a%p;b=b%p;
if (b==1||p==1)
{
cout<<"0\n";
continue;
}
int gcd=1;
int cnt=0;
bool flag=0;
while (1)
{
int tg=__gcd(a,p);
if (tg==1) break;
++cnt;
if (b%tg!=0)
{
cout<<"No Solution\n";
flag=1;
break;
}
gcd*=a/tg;b/=tg;p/=tg;
gcd%=p;
}
if (flag) continue;
for (int i=1;i<=cnt;++i)
{
if (qp(a,i)==b)
{
cout<<i<<'\n';
flag=1;
break;
}
}
if (flag) continue;
int ans=bsgs(a,b*inv(gcd)%p,p);
if (ans==-1) cout<<"No Solution\n";
else cout<<ans+cnt<<'\n';
}
fclose(stdin);
fclose(stdout);
return 0;
}