From 7a52adeeee63725f73fa9a89e911103a97846b60 Mon Sep 17 00:00:00 2001 From: Baoshuo Date: Mon, 25 Apr 2022 15:18:15 +0800 Subject: [PATCH] =?UTF-8?q?1298.=20=E6=9B=B9=E5=86=B2=E5=85=BB=E7=8C=AA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit https://www.acwing.com/problem/content/submission/code_detail/13684946/ --- AcWing/1298/1298.cpp | 52 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 AcWing/1298/1298.cpp diff --git a/AcWing/1298/1298.cpp b/AcWing/1298/1298.cpp new file mode 100644 index 00000000..c8a6230c --- /dev/null +++ b/AcWing/1298/1298.cpp @@ -0,0 +1,52 @@ +#include + +using std::cin; +using std::cout; +const char endl = '\n'; + +const int N = 1e5 + 5; + +int n; +long long a[N], b[N]; + +long long exgcd(long long a, long long b, long long &x, long long &y) { + if (!b) { + x = 1, y = 0; + return a; + } + + long long g = exgcd(b, a % b, y, x); + y -= a / b * x; + + return g; +} + +long long CRT() { + long long mod = 1, ans = 0; + + for (int i = 1; i <= n; i++) { + mod *= a[i]; + } + + for (int i = 1; i <= n; i++) { + long long m = mod / a[i], x, y; + exgcd(m, a[i], x, y); + ans = (ans + b[i] * m * x % mod) % mod; + } + + return (ans % mod + mod) % mod; +} + +int main() { + std::ios::sync_with_stdio(false); + + cin >> n; + + for (int i = 1; i <= n; i++) { + cin >> a[i] >> b[i]; + } + + cout << CRT() << endl; + + return 0; +}