From fafbbfd4f4ed27156a3405f3261396db7f204cf3 Mon Sep 17 00:00:00 2001 From: Baoshuo Date: Sun, 2 Oct 2022 13:40:55 +0800 Subject: [PATCH] =?UTF-8?q?P5656=20=E3=80=90=E6=A8=A1=E6=9D=BF=E3=80=91?= =?UTF-8?q?=E4=BA=8C=E5=85=83=E4=B8=80=E6=AC=A1=E4=B8=8D=E5=AE=9A=E6=96=B9?= =?UTF-8?q?=E7=A8=8B=20(exgcd)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit https://www.luogu.com.cn/record/88197267 --- Luogu/P5656/P5656.cpp | 67 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 Luogu/P5656/P5656.cpp diff --git a/Luogu/P5656/P5656.cpp b/Luogu/P5656/P5656.cpp new file mode 100644 index 00000000..fe170aff --- /dev/null +++ b/Luogu/P5656/P5656.cpp @@ -0,0 +1,67 @@ +#include +#include + +using std::cin; +using std::cout; +const char endl = '\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; +} + +int main() { + std::ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + + cin >> t; + + while (t--) { + long long a, b, c, x, y; + + cin >> a >> b >> c; + + long long g = exgcd(a, b, x, y); + + if (c % g) { + cout << -1 << endl; + } else { // c % g == 0 + x *= c / g; + y *= c / g; + + long long p = b / g, + q = a / g; + + if (x < 0) { + long long k = std::ceil((1.0 - x) / p); + x += p * k; + y -= q * k; + } else { // x >= 0 + long long k = (x - 1) / p; + x -= p * k; + y += q * k; + } + + if (y > 0) { + cout << (y - 1) / q + 1 << ' ' + << x << ' ' + << (y - 1) % q + 1 << ' ' + << x + (y - 1) / q * p << ' ' + << y << endl; + } else { // y <= 0 + cout << x << ' ' + << y + q * static_cast(std::ceil((1.0 - y) / q)) << endl; + } + } + } + + return 0; +}