From 017a3770b3cd663652bc08d0b9abf360c849397c Mon Sep 17 00:00:00 2001 From: Baoshuo Date: Sat, 29 Oct 2022 11:22:01 +0800 Subject: [PATCH] =?UTF-8?q?P4777=20=E3=80=90=E6=A8=A1=E6=9D=BF=E3=80=91?= =?UTF-8?q?=E6=89=A9=E5=B1=95=E4=B8=AD=E5=9B=BD=E5=89=A9=E4=BD=99=E5=AE=9A?= =?UTF-8?q?=E7=90=86=EF=BC=88EXCRT=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit https://www.luogu.com.cn/record/92007494 --- Luogu/P4777/P4777.cpp | 37 ++++++++++++++++--------------------- 1 file changed, 16 insertions(+), 21 deletions(-) diff --git a/Luogu/P4777/P4777.cpp b/Luogu/P4777/P4777.cpp index 53f3db17..82b80f6e 100644 --- a/Luogu/P4777/P4777.cpp +++ b/Luogu/P4777/P4777.cpp @@ -5,25 +5,21 @@ using std::cin; using std::cout; const char endl = '\n'; -const int N = 1e5 + 5; - int n; long long a1, b1, a2, b2; -long long exgcd(long long a, long long b, long long& x, long long& y) { +long long exgcd(long long a, long long b, long long &x, long long &y) { if (!b) { - x = 1; - y = 0; + x = 1, y = 0; + return a; } long long g = exgcd(b, a % b, y, x); - y -= a / b * x; - return g; -} -long long lcm(long long a, long long b) { - return a / std::__gcd(a, b) * b; + y -= a / b * x; + + return g; } int main() { @@ -33,18 +29,17 @@ int main() { cin >> n; for (int i = 1; i <= n; i++) { - // $x \equiv a_i (mod b_i)$ - cin >> a2 >> b2; + cin >> b2 >> a2; if (i == 1) { - a1 = a2; b1 = b2; + a1 = a2; } else { long long x, y, - g = exgcd(a1, a2, x, y), - c = b2 - b1, - m1 = a2 / g, - m2 = lcm(a1, a2); + g = exgcd(b1, b2, x, y), + c = a2 - a1, + m1 = b2 / g, + m2 = b1 / g * b2; if (c % g) { cout << -1 << endl; @@ -52,13 +47,13 @@ int main() { exit(0); } - x = (static_cast<__int128>(x) * c / g % m1 + m1) % m1; - b1 = ((a1 * x + b1) % m2 + m2) % m2; - a1 = m2; + x = static_cast<__int128>(x) * c / g % m1; + a1 = ((a1 + b1 * x) % m2 + m2) % m2; + b1 = m2; } } - cout << b1 % a1 << endl; + cout << a1 % b1 << endl; return 0; }