//g++ 1.cpp -std=c++14 -O2 -I . #include using namespace std; #include using namespace atcoder; using ll = long long; using ld = long double; using vi = vector; using vvi = vector; using vll = vector; using vvll = vector; using vld = vector; using vvld = vector; #define fi first #define se second #define pb push_back #define pq_big(T) priority_queue,less> #define pq_small(T) priority_queue,greater> #define all(a) a.begin(),a.end() #define rep(i,start,end) for(ll i=start;i<(ll)(end);i++) #define per(i,start,end) for(ll i=start;i>=(ll)(end);i--) constexpr ll mod = 998244353; //p*q行列aとr*s行列bの積(mod) template vector> matmulti(const vector> &a,const vector> &b,X mod){ int p=a.size(),q=a[0].size(),r=b.size(),s=b[0].size(); if(q!=r){ cout<<"WARNING : SIZE ERROR"<> res(p,vector(s,0)); for(int i=0;i>a>>b>>p>>q; if(b==0){ if(a==0){ cout<<2<<"\n"; return 0; } //pow1[i] := A^i //pow2[i] := A^{10^5 i} ll pow1[110000],pow2[110000]; pow1[0]=1,pow2[0]=1; map m; m[1]=0; rep(i,1,110000){ pow1[i]=pow1[i-1]*a; pow1[i]%=mod; m[pow1[i]]=i; } rep(i,1,110000){ pow2[i]=pow2[i-1]*pow1[100000]; pow2[i]%=mod; } rep(i,0,110000){ ll inv=inv_mod(pow2[i],mod); ll z=(p*inv)%mod; ll ans=-1; if(z==1){ ans=100000*i; } else if(m[z]!=0){ ans=100000*i+m[z]; } if(ans>=2){ cout< pow1,pow2; pow1.emplace_back(e),pow2.emplace_back(e); rep(i,0,110000){ pow1.emplace_back(matmulti(pow1[i],m,mod)); } rep(i,0,110000){ pow2.emplace_back(matmulti(pow2[i],pow1[100000],mod)); } map seen; map val; rep(i,0,110000){ auto x=matmulti(pow1[i],start,mod); seen[x]=1; val[x]=i; } rep(i,0,110000){ //x=m^{10^5 i} //inv_x = x^-1 vvll x=pow2[i]; ll det=x[0][0]*x[1][1]-x[0][1]*x[1][0]; det=((det%mod)+mod)%mod; ll inv_det=inv_mod(det,mod); vvll inv_x(2,vll(2)); inv_x[0][0]=(inv_det*x[1][1])%mod,inv_x[0][1]=(inv_det*(mod-x[0][1]))%mod,inv_x[1][0]=(inv_det*(mod-x[1][0]))%mod,inv_x[1][1]=(inv_det*x[0][0])%mod; auto z=matmulti(inv_x,end,mod); ll ans=-1; if(seen[z]==1){ ans=100000*i+val[z]; } if(ans>=2){ cout<