From 7f2397c66513e5467a12161ff6fd89ad9b8cff2f Mon Sep 17 00:00:00 2001 From: Baoshuo Date: Tue, 10 May 2022 15:32:13 +0800 Subject: [PATCH] =?UTF-8?q?878.=20=E7=BA=BF=E6=80=A7=E5=90=8C=E4=BD=99?= =?UTF-8?q?=E6=96=B9=E7=A8=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit https://www.acwing.com/problem/content/submission/code_detail/14083066/ --- AcWing/878/878.cpp | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 AcWing/878/878.cpp diff --git a/AcWing/878/878.cpp b/AcWing/878/878.cpp new file mode 100644 index 00000000..320c728f --- /dev/null +++ b/AcWing/878/878.cpp @@ -0,0 +1,40 @@ +#include + +using std::cin; +using std::cout; +const char endl = '\n'; + +int n, a, b, m, x, y; + +int exgcd(int a, int b, int &x, int &y) { + if (!b) { + x = 1, y = 0; + return a; + } + + int d = exgcd(b, a % b, y, x); + + y -= a / b * x; + + return d; +} + +int main() { + std::ios::sync_with_stdio(false); + + cin >> n; + + while (n--) { + cin >> a >> b >> m; + + int d = exgcd(a, m, x, y); + + if (b % d) { + cout << "impossible" << endl; + } else { + cout << 1ll * b / d * x % m << endl; + } + } + + return 0; +}