#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; typedef long long int ll; typedef pair P; const ll MOD=1e9+7; ll inv(ll a, ll m){ ll b=m, x=1, y=0; while(b>0){ ll t=a/b; swap(a-=t*b, b); swap(x-=t*y, y); } return (x%m+m)%m; } int main() { int n; cin>>n; int a[100010], b[100010]; for(int i=0; i>a[i]; a[i]--; } for(int i=0; i>b[i]; b[i]--; } int p[100010], q[100010]; fill(p, p+n, -1); fill(q, q+n, -1); vector va[100010], vb[100010]; int idx=0; for(int i=0; i> mp; for(int i=0; i> mp1; for(auto i:v){ ll rp=ra[i], rq=rb[i]; ll s=(rq-rp%g+g)%g; rq=(rq-s+cq)%cq; ll x=inv(cp/g, cq/g)*(((rq-rp)/g)%(cq/g)+cq/g)%(cq/g); mp1[s].push_back(x*cp+rp); } for(auto pr2:mp1){ auto w=pr2.second; sort(w.begin(), w.end()); w.push_back(w[0]+cp*cq/g); for(int i=0; i