#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #define popcount __builtin_popcount using namespace std; using namespace atcoder; typedef long long int ll; typedef pair P; ll p; using arr=array; using Mat=array; Mat matmul(Mat a, Mat b){ Mat c={}; for(int i=0; i<2; i++){ for(int j=0; j<2; j++){ for(int k=0; k<2; k++){ (c[i][j]+=a[i][k]*b[k][j])%=p; } } } return c; } Mat matpow(Mat a, ll k){ Mat ap=a, ans={}; ans[0][0]=ans[1][1]=1; while(k){ if(k&1) ans=matmul(ap, ans); ap=matmul(ap, ap); k>>=1; } return ans; } Mat e; vector

fac(bool myon){ map mp; vector

ret; ll x=p-1; for(ll i=2; i*i<=x; i++){ if(x%i==0){ int f=0; while(x%i==0){ x/=i; f++; } mp[i]+=f; } } if(x>1) mp[x]++; if(!myon){ for(auto q:mp) ret.push_back(q); return ret; } x=p+1; for(ll i=2; i*i<=x; i++){ if(x%i==0){ int f=0; while(x%i==0){ x/=i; f++; } mp[i]+=f; } } if(x>1) mp[x]++; for(auto q:mp) ret.push_back(q); return ret; } vector order(Mat a){ ll x=(p-1)*(p+1); bool myon=0; if(matpow(a, x)!=e){ x=p*(p-1); }else{ myon=1; } vector

v=fac(myon); vector ret; for(auto q:v){ ll r=q.first; int f=q.second; ll y=1; for(int i=0; i1) ret.push_back(y); } if(!myon) ret.push_back(p); return ret; } ll discretelog(Mat a, Mat b, ll d){ ll sq=1; while(sq*sq> vp(sq); vp[0]=make_pair(e, 0); for(int i=1; i x, pair y){ for(int i=0; i<2; i++){ for(int j=0; j<2; j++){ if(x.first[i][j]!=y.first[i][j]) return x.first[i][j]0){ ll t=a/b; swap(a-=t*b, b); swap(x-=t*y, y); } return (x%m+m)%m; } ll powmod(ll a, ll k, ll mod){ ll ap=a, ans=1; while(k){ if(k&1){ ans*=ap; ans%=mod; } ap=ap*ap; ap%=mod; k>>=1; } return ans; } ll discretelog0(ll x, ll y, ll m){ ll p=1%m; for(int i=0; i<=30; i++){ if(p==y) return i; (p*=x)%=m; } ll g=gcd(x, m); ll m1=m; for(ll i=2; i*i<=g; i++){ if(g%i==0){ while(g%i==0) g/=i; while(m1%i==0) m1/=i; } } if(g>1){ while(m1%g==0) m1/=g; } if(y%(m/m1)!=0) return -1; x%=m1, y%=m1; ll d=1; while(d*d mp; //yx^(-b) ll y1=y; for(ll i=0; isecond; } (q*=xd)%=m1; } return -1; } int main() { p=998244353; using mint=modint998244353; ll A, B, P, Q; cin>>A>>B>>P>>Q; if(A*A-4*B==0){ if(A==0){ cout<<2<>a[0][0]>>a[0][1]>>a[1][0]>>a[1][1]>>b[0][0]>>b[0][1]>>b[1][0]>>b[1][1]; a[0][0]=A, a[0][1]=(p-B)%p, a[1][0]=1, a[1][1]=0; Mat b1; b1[0][0]=mint(A*P-B*Q).val(), b1[0][1]=P, b1[1][0]=P, b1[1][1]=Q; Mat b2; b2[0][0]=2, b2[0][1]=mint(-A).val(), b2[1][0]=b2[0][1], b2[1][1]=mint(A*A-2*B).val(); mint D=mint(A*A-4*B); for(int i=0; i<2; i++) for(int j=0; j<2; j++){ b2[i][j]=(mint(b2[i][j])/D).val(); } b=matmul(b1, b2); if(a==b){ cout<<2< vd=order(a); if(vd.empty()){ cout<<-1< vf(k); for(int i=0; i