#include #include #include #include #include #include using namespace std; // 中国剰余定理 // x = b_1 (mod_1), ..., x = b_k (mod_k), ... を満たす x を // 0 <= x < lcm(mod_1, mod_2, ..., mod_k, ...) の範囲で求める template struct CRT { pair NIL; CRT() : NIL(-1, -1) {} NumType extgcd(NumType a, NumType b, NumType &p, NumType &q) { if(b == 0) { p = 1, q = 0; return a; } NumType d = extgcd(b, a%b, q, p); q -= a / b * p; return d; } pair solve(NumType b1, NumType mod1, NumType b2, NumType mod2) { NumType p, q; NumType d = extgcd(mod1, mod2, p, q); if((b2 - b1) % d != 0) return NIL; NumType s = (b2 - b1) / d; NumType t = (s * p % (mod2 / d)); // get lcm NumType lc = mod1 / d * mod2; NumType so = (b1 + mod1 * t) % lc; (so += lc) %= lc; return make_pair(so, lc); } // m, mod の vector をもらって、 // CRT の解を (x, lcm(mod_1, mod_2, ..., mod_k, ...)) の形で返す pair solve(vector m, vector mod) { assert(m.size() == mod.size()); NumType so = 0, lc = 1; for(size_t i=0; i LIMIT or so < 0) { return NIL; } } return make_pair(so, lc); } }; void yuki_186() { using ll = long long int; vector b(3), mod(3); for(int i=0; i<3; i++) { cin >> b[i] >> mod[i]; } CRT crt; auto ans = crt.solve(b, mod); cout << ans.first + (ans.first == 0 ? ans.second : 0) << endl; } int main() { yuki_186(); return 0; }