int exbsgs(int a,int b,int p)
{
int tot = 1,cnt = 0,d = gcd(a,p);
b %= p; //防止超时
if (b==1)
return 0;
while (d>1)//直到gcd(a,p)==1
{
if (b%d) //b不能被d整除,则无解
return -1;
b /= d;
p /= d;
tot *= (a/d);
tot %= p;
cnt++;
if (tot==b)
return cnt;
d = gcd(a,p);
}
int x = ex_inv(tot,p);
b*=x;
b%=p;
int res = bsgs(a,b,p);
if(~res)
return res+cnt;
return res;
}