From f05c95b8e344bb52b035908156eb2a96cc8646cf Mon Sep 17 00:00:00 2001 From: Baoshuo Date: Thu, 13 Oct 2022 19:48:48 +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/89726444 --- Luogu/P4777/P4777.cpp | 64 +++++++++++++++++++++++++++++++++++ Luogu/P4777/data/P4777_15.in | 3 ++ Luogu/P4777/data/P4777_15.out | 3 ++ 3 files changed, 70 insertions(+) create mode 100644 Luogu/P4777/P4777.cpp create mode 100644 Luogu/P4777/data/P4777_15.in create mode 100644 Luogu/P4777/data/P4777_15.out diff --git a/Luogu/P4777/P4777.cpp b/Luogu/P4777/P4777.cpp new file mode 100644 index 00000000..53f3db17 --- /dev/null +++ b/Luogu/P4777/P4777.cpp @@ -0,0 +1,64 @@ +#include +#include + +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) { + 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 lcm(long long a, long long b) { + return a / std::__gcd(a, b) * b; +} + +int main() { + std::ios::sync_with_stdio(false); + cin.tie(nullptr); + + cin >> n; + + for (int i = 1; i <= n; i++) { + // $x \equiv a_i (mod b_i)$ + cin >> a2 >> b2; + + if (i == 1) { + a1 = a2; + b1 = b2; + } else { + long long x, y, + g = exgcd(a1, a2, x, y), + c = b2 - b1, + m1 = a2 / g, + m2 = lcm(a1, a2); + + if (c % g) { + cout << -1 << endl; + + exit(0); + } + + x = (static_cast<__int128>(x) * c / g % m1 + m1) % m1; + b1 = ((a1 * x + b1) % m2 + m2) % m2; + a1 = m2; + } + } + + cout << b1 % a1 << endl; + + return 0; +} diff --git a/Luogu/P4777/data/P4777_15.in b/Luogu/P4777/data/P4777_15.in new file mode 100644 index 00000000..f7dbe092 --- /dev/null +++ b/Luogu/P4777/data/P4777_15.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:b28d9bac7d5959c92c3fdc68fec293d5f5b78acd2efb795a12ca977d4bc6c88e +size 1550359 diff --git a/Luogu/P4777/data/P4777_15.out b/Luogu/P4777/data/P4777_15.out new file mode 100644 index 00000000..37478da7 --- /dev/null +++ b/Luogu/P4777/data/P4777_15.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:8b0cc1ae37e9818c213ed2ce318563e325f355f3658281638aa271ba02bcda74 +size 14