#ifdef LOCAL111 #define _GLIBCXX_DEBUG #else #define NDEBUG #endif #define _USE_MATH_DEFINES #include const int INF = 1e9; using namespace std; template ostream& operator<< (ostream& os, const pair& p) { cout << '(' << p.first << ' ' << p.second << ')'; return os; } const int mod = 1e18+7; #define endl '\n' #define ALL(a) (a).begin(),(a).end() #define SZ(a) int((a).size()) #define FOR(i,a,b) for(int i=(a);i<(b);++i) #define RFOR(i,a,b) for (int i=(b)-1;i>=(a);i--) #define REP(i,n) FOR(i,0,n) #define RREP(i,n) for (int i=(n)-1;i>=0;i--) #ifdef LOCAL111 #define DEBUG(x) cout<<#x<<": "<<(x)< void dpite(T a, T b){ for(T ite = a; ite != b; ite++) cout << (ite == a ? "" : " ") << *ite; cout << endl;} #else #define DEBUG(x) true template void dpite(T a, T b){ return; } #endif #define F first #define S second #define SNP string::npos #define WRC(hoge) cout << "Case #" << (hoge)+1 << ": " template void pite(T a, T b){ for(T ite = a; ite != b; ite++) cout << (ite == a ? "" : " ") << *ite; cout << endl;} template bool chmax(T& a, T b){if(a < b){a = b; return true;} return false;} template bool chmin(T& a, T b){if(a > b){a = b; return true;} return false;} template long long gcd(T x, T y){ return y==0 ? x : gcd(y, x%y); } template long long lcm(T x, T y){ return x/gcd(x,y)*y; } using Integer = long long; //res.first*a+res.second*b == 1 となるresを返す (a,bは互いに素) pair extgcd(Integer a,Integer b) { if(b==1){ return pair(0,1); } pair t=extgcd(b,a%b); return pair(t.second,t.first-a/b*t.second); } //modの逆元を返す Integer inverse(Integer a,Integer modl) { return (extgcd(modl,a).second+modl)%modl; } // x = second (mod first) 0以上の最小解 Integer garner_gen(const vector>& ex) { int n = ex.size(); Integer res = 0; Integer k = 1; for(int i = 0; i < n; ++i) { Integer x, m; tie(m,x) = ex[i]; x = (x%m+m)%m; Integer g = gcd(k,m); if((x-res)%g != 0) return -1; Integer inv = inverse(k/g,m/g); Integer v = ((x-res)/g%m+m)%m*inv%m; Integer nk = k/g*m; res = (res+v*k%nk)%nk; k = k/g*m; } return res; } Integer garner_gen(const vector& x, const vector& mod){ int n = x.size(); vector> v(n); for(int i = 0; i < n; ++i) { v[i] = {mod[i],x[i]}; } return garner_gen(v); } //あんま検証してないよ Integer garner(const vector>& ex) { int n = ex.size(); Integer res = 0; Integer k = 1; for(int i = 0; i < n; ++i) { Integer x, m; tie(m,x) = ex[i]; // x = (x%m+m)%m; Integer inv = inverse(k,m); Integer v = (x-res)*inv%m; res = (res+v*k); k = k*m; } return res; } Integer garner(const vector& x, const vector& mod){ int n = x.size(); vector> v(n); for(int i = 0; i < n; ++i) { v[i] = {mod[i],x[i]}; } return garner(v); } Integer garner_mod(vector> ex, Integer mod) { int n = ex.size(); Integer res = 0; // Integer k = 1; vector> memo(n); for(int i = 0; i < n; ++i) { Integer x, m; ex[i].S %= ex[i].F; tie(m,x) = ex[i]; DEBUG(m); DEBUG(x); // x = (x%m+m)%m; Integer k = 1; Integer kk = 1; Integer ress = 0; for(int j = 0; j < i; ++j) { k = k*(ex[j].F%m)%m; ress = (ress+memo[j].F*kk%m)%m; kk = kk*(ex[j].F%mod)%mod; } Integer inv = inverse(k,m); Integer v = (x-ress+m)*inv%m; DEBUG(v); DEBUG(k); res = (res+v*kk%mod)%mod; memo[i] = {v,kk}; DEBUG(res); } return res; } Integer garner_mod(const vector& x, const vector& mod, Integer modi){ int n = x.size(); vector> v(n); for(int i = 0; i < n; ++i) { v[i] = {mod[i],x[i]}; } return garner_mod(v,modi); } template unordered_map primeFactorize(T x){ unordered_map res; for(T i = 2; i*i <= x; i++){ while(x%i == 0){ res[i]++; x /= i; } } if(x != 1) res[x]++; return res; } typedef long long int LL; typedef unsigned long long ULL; typedef pair P; void ios_init(){ //cout.setf(ios::fixed); //cout.precision(12); #ifdef LOCAL111 return; #endif ios::sync_with_stdio(false); cin.tie(0); } int main() { ios_init(); int n = 3; // while(cin >> n){ { using P = pair; vector x(n),y(n); REP(i,n) cin >> x[i] >> y[i]; vector> facs(n); unordered_map> mp; REP(i,n){ facs[i] = primeFactorize(y[i]); for(auto&& e : facs[i]) { mp[e.F].emplace_back(e.S,i); } } bool f = true; for(auto&& e : mp) { DEBUG(e.F); sort(ALL(e.S)); int mod = pow(e.F,e.S.back().F); int val = x[e.S.back().S]; REP(i,SZ(e.S)-1){ DEBUG(y[e.S[i].S]); int modt = pow(e.F,e.S[i].F); int valt = x[e.S[i].S]%modt; if(val%modt != valt){ DEBUG(i); f = false; // break; } y[e.S[i].S] /= modt; x[e.S[i].S] %= y[e.S[i].S]; } } dpite(ALL(x)); dpite(ALL(y)); if(!f){ cout << -1 << endl; }else{ bool zf = true; for(int i = 0; i < n; ++i) { if(x[i] != 0) zf = false; } LL ans = 1; if(zf){ for(int i = 0; i < n; ++i) { ans *= y[i]; // ans %= mod; } }else{ vector

ex(n); REP(i,n) ex[i] = {y[i],x[i]}; sort(ALL(ex)); ex.erase(unique(ALL(ex)),ex.end()); if(binary_search(ALL(ex),P({1,0}))){ ex.erase(lower_bound(ALL(ex),P(0,1))); } if(binary_search(ALL(ex),P({1,1}))){ ex.erase(lower_bound(ALL(ex),P(1,1))); } dpite(ALL(ex)); ans = garner_gen(ex); } cout << ans << endl; } } return 0; }