#include #include using namespace std; using mint=atcoder::modint998244353; #include #include int BSGS(long long X,long long Y,const int P0) //k s.t. X^k=Y mod P of -1 //0^0=1 { X%=P0; Y%=P0; if(P0==1)return 0; if(X==0)return Y==1?0:Y==0?1:-1; int g=1; for(int i=P0;i>1;i>>=1)g=g*X%P0; //gcd for(int p=P0;p;) { int t=g%p; g=p; p=t; } int t=1,fst=0; while(t%g) { if(t==Y)return fst; t=t*X%P0; fst++; } if(Y%g)return -1; t/=g; Y/=g; const int P=P0/g; int sqP=0,Xm=1; while(sqP*sqP >tb(sqP); for(int i=0,e=Y;i >::iterator it=lower_bound(tb.begin(),tb.end(),make_pair(e,-sqP)); if(it!=tb.end()&&it->first==e)return fst+s+it->second; } return -1; } #include template struct Matrix{ array,N>dat; array&operator[](int i){return dat[i];} const array&operator[](int i)const{return dat[i];} static Matrix eye(){ Matrix res; for(int i=0;i>=1)if(n&1)res=res*a; return res; } }; using mat=Matrix; int A,B,P,Q; main() { cin>>A>>B>>P>>Q; if(B==0) { int k=BSGS(A,P,998244353); if(k<2)k+=998244353-1; cout<,int> >tb(sqP); paire=make_pair(P,Q); for(int i=0;ifirst==e) { long N=s+it->second-1; if(N>=2) { cout<