0
1
mirror of https://git.sb/baoshuo/OI-codes.git synced 2024-11-08 14:58:48 +00:00

P5656 【模板】二元一次不定方程 (exgcd)

https://www.luogu.com.cn/record/88197267
This commit is contained in:
Baoshuo Ren 2022-10-02 13:40:55 +08:00
parent a98b7f0ed3
commit fafbbfd4f4
Signed by: baoshuo
GPG Key ID: 00CB9680AB29F51A

67
Luogu/P5656/P5656.cpp Normal file
View File

@ -0,0 +1,67 @@
#include <iostream>
#include <cmath>
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<long long>(std::ceil((1.0 - y) / q)) << endl;
}
}
}
return 0;
}