Search for a command to run...
Progressive hints first, then the full explanation and implementation when you're ready to cash out.
Review status
AI-generated and still unreviewed. Double-check the details before internalizing them.
Hints
Open only as much as you need to keep the solve alive.
Forget the geometry. The equation is just . Ask yourself: which integers can be represented in the form ?
Bézout is the whole game here: all values are multiples of , and every multiple of can be represented.
So if , the answer is impossible. If , you only need one solution to .
Run extended Euclid on and to get , then flip or if the original coefficient was negative.
After sign-fixing, you have . Multiply both coefficients by : The bounds are generous enough; no shifting along the line is needed.
The line equation
is really the linear Diophantine equation
So this is not geometry. It is Bézout wearing a fake mustache.
Let
For any integers , both and are divisible by , so is also divisible by . Therefore a necessary condition is
which is the same as .
Bézout's identity says this condition is also sufficient: there exist integers such that
Then multiplying both sides by
gives
So one valid answer is
Use the extended Euclidean algorithm. It naturally gives coefficients for nonnegative inputs:
Then fix signs:
After that,
Finally multiply by .
The problem asks for coordinates between and . Looks scary, but it is basically a neon sign saying use long long.
For non-degenerate cases, extended Euclid gives coefficients with size roughly bounded by the opposite coefficient divided by :
Since
we get
and similarly for . If or , the solution is even smaller. So the direct Bézout solution is safe.
Extended Euclid runs in
time. Memory usage is due to recursion, which is tiny here.
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void setIO() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
}
ll extgcd(ll a, ll b, ll &x, ll &y) {
if (b == 0) {
x = 1;
y = 0;
return a;
}
ll x1, y1;
ll g = extgcd(b, a % b, x1, y1);
x = y1;
y = x1 - (a / b) * y1;
return g;
}
int main() {
setIO();
ll A, B, C;
cin >> A >> B >> C;
ll a = A < 0 ? -A : A;
ll b = B < 0 ? -B : B;
ll x, y;
ll g = extgcd(a, b, x, y);
if (C % g != 0) {
cout << -1;
return 0;
}
if (A < 0) x = -x;
if (B < 0) y = -y;
ll mult = -C / g;
cout << x * mult << ' ' << y * mult;
return 0;
}#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void setIO() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
}
ll extgcd(ll a, ll b, ll &x, ll &y) {
if (b == 0) {
x = 1;
y = 0;
return a;
}
ll x1, y1;
ll g = extgcd(b, a % b, x1, y1);
x = y1;
y = x1 - (a / b) * y1;
return g;
}
int main() {
setIO();
ll A, B, C;
cin >> A >> B >> C;
ll a = A < 0 ? -A : A;
ll b = B < 0 ? -B : B;
ll x, y;
ll g = extgcd(a, b, x, y);
if (C % g != 0) {
cout << -1;
return 0;
}
if (A < 0) x = -x;
if (B < 0) y = -y;
ll mult = -C / g;
cout << x * mult << ' ' << y * mult;
return 0;
}