#line 1 "test\\yukicoder\\448.test.cpp" #include #define PROBLEM "https://yukicoder.me/problems/448" #line 1 "lib\\math\\crt_mod.hpp" #line 1 "lib\\util.hpp" #include #include #include #include namespace tifa_libs { using i32 = int32_t; using u32 = uint32_t; using i64 = int64_t; using u64 = uint64_t; using i128 = __int128_t; using u128 = __uint128_t; } // namespace tifa_libs #line 5 "lib\\math\\crt_mod.hpp" #line 1 "lib\\math\\inverse.hpp" #line 5 "lib\\math\\inverse.hpp" #line 1 "lib\\math\\inv_gcd.hpp" #line 5 "lib\\math\\inv_gcd.hpp" #line 1 "lib\\math\\exgcd.hpp" #line 5 "lib\\math\\exgcd.hpp" #include namespace tifa_libs::math { // @return tuple(g, x, y) s.t. g = gcd(a, b), xa + yb = g, |x| + |y| is the minimal (primary) and x <= y (secondarily) constexpr std::tuple exgcd(i64 a, i64 b) { i64 x1 = 1, x2 = 0, x3 = 0, x4 = 1; while (b) { i64 c = a / b; std::tie(x1, x2, x3, x4, a, b) = std::make_tuple(x3, x4, x1 - x3 * c, x2 - x4 * c, b, a - b * c); } return {a, x1, x2}; } } // namespace tifa_libs::math #line 7 "lib\\math\\inv_gcd.hpp" namespace tifa_libs::math { constexpr std::pair inv_gcd(u64 n, u64 mod) { auto [g, x, y] = exgcd((i64)(n % mod), (i64)mod); return {g, x > 0 ? (u64)x : (u64)(x % (i64)mod + (i64)mod) % mod}; } } // namespace tifa_libs::math #line 7 "lib\\math\\inverse.hpp" namespace tifa_libs::math { constexpr u64 inverse(u64 n, u64 mod) { auto [g, x] = inv_gcd(n % mod, mod); assert(g == 1); return x; } } // namespace tifa_libs::math #line 1 "lib\\math\\safe_mod.hpp" #line 5 "lib\\math\\safe_mod.hpp" namespace tifa_libs::math { template , int> = 0> constexpr T safe_mod(T x, std::make_unsigned_t mod) { return ((x %= (T)mod) < 0 ? x + (T)mod : x); } } // namespace tifa_libs::math #line 8 "lib\\math\\crt_mod.hpp" #include #include #include namespace tifa_libs::math { inline std::optional> crt_mod(const std::vector &a, const std::vector &m, const u32 mod) { assert(a.size() == m.size()); const size_t n = a.size(); std::vector m_cpy(m); for (size_t i = 0; i < n; ++i) { u32 &mi = m_cpy[i]; for (size_t j = 0; j < i; ++j) { u32 &mj = m_cpy[j], d = std::gcd(mi, mj); if (d == 1) continue; if (safe_mod(a[i], d) != safe_mod(a[j], d)) return {}; mi /= d; mj /= d; if (u32 k = std::gcd(mi, d); k != 1) while (d % k == 0) { mi *= k; d /= k; } mj *= d; } } m_cpy.push_back(mod); std::vector pp(n + 1, 1), res(n + 1); for (size_t i = 0; i < n; ++i) { i64 u = safe_mod((safe_mod(a[i], m_cpy[i]) - res[i]) * (i64)inverse((u64)pp[i], m_cpy[i]), m_cpy[i]); for (size_t j = i + 1; j <= n; ++j) { res[j] = (i32)((res[j] + u * pp[j]) % m_cpy[j]); pp[j] = (i32)((i64)pp[j] * m_cpy[i] % m_cpy[j]); } } return std::make_pair(res.back(), pp.back()); } } // namespace tifa_libs::math #line 5 "test\\yukicoder\\448.test.cpp" #include #line 8 "test\\yukicoder\\448.test.cpp" int main() { size_t n; std::cin >> n; std::vector r(n); std::vector m(n); for (size_t i = 0; i < n; ++i) std::cin >> r[i] >> m[i]; auto res = tifa_libs::math::crt_mod(r, m, 1'000'000'007); if (!res) { std::cout << "-1\n"; return 0; } std::cout << ((size_t)std::count(r.begin(), r.end(), 0) == n ? res->second : res->first) << '\n'; return 0; }