From 4850a278560ae4c3e10aa9d51561e0aab8065c4e Mon Sep 17 00:00:00 2001 From: Baoshuo Date: Sun, 24 Apr 2022 21:09:45 +0800 Subject: [PATCH] =?UTF-8?q?P1495=20=E3=80=90=E6=A8=A1=E6=9D=BF=E3=80=91?= =?UTF-8?q?=E4=B8=AD=E5=9B=BD=E5=89=A9=E4=BD=99=E5=AE=9A=E7=90=86(CRT)/?= =?UTF-8?q?=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.luogu.com.cn/record/74538615 --- Luogu/P1495/P1495.cpp | 52 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 Luogu/P1495/P1495.cpp diff --git a/Luogu/P1495/P1495.cpp b/Luogu/P1495/P1495.cpp new file mode 100644 index 00000000..c8a6230c --- /dev/null +++ b/Luogu/P1495/P1495.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; +}