A line on the plane is described by an equation Ax + By + C = 0. You are to find any point on this line, whose coordinates are integer numbers from - 5·1018 to 5·1018 inclusive, or to find out that such points do not exist.
The first line contains three integers A, B and C ( - 2·109 ≤ A, B, C ≤ 2·109) — corresponding coefficients of the line equation. It is guaranteed that A2 + B2 > 0.
If the required point exists, output its coordinates, otherwise output -1.
2 5 3
6 -3
思路:將ax+by+c=0,化爲ax+by=-c/gcd(a,b)*gcd(a,b),套拓展歐幾里得就可以解出了
上代碼:
/*
對於不完全爲0的非負整數a,b,gcd(a, b)表示a, b的最大
公約數,必定存在整數對x,y,滿足a*x+b*y==gcd(a, b)
求解不定方程;如a*x+b*y=c; 已知a, b, c的值求x和y的值
a*x+b*y=gcd(a, b)*c/gcd(a, b);
最後轉化爲 a*x/(c/gcd(a, b))+b*y/(c/gcd(a, b))=gcd(a, b);
最後求出的解x0,y0乘上c/gcd(a, b)就是最終的結果了
x1=x0*c/gcd(a, b);
y1=y0*c/gcd(a, b);
*/
#include<iostream>
#include<cmath>
#include<cstring>
#include<stack>
#include<cstdio>
#include<string>
#include<vector>
#include<algorithm>
#define M 20010
using namespace std;
#define ll long long
#define inf 0x3f3f3f3f
#define maxn 1000010
//x1 = y2, y1 = x2 - a / b*y2;
ll exgcd(ll a, ll b, ll &x, ll &y)
{
if (b == 0)
{
x = 1, y = 0;
//cout << x << ' ' << y << endl;
return a;//最小公約數
}
ll g = exgcd(b, a%b, x, y);
ll t;
t = x, x = y, y = t - (a / b)*y;
//cout << x <<' '<< y << endl;
return g;
}
int main()
{
ll a, b, c, x, y;
cin >> a >> b >> c;
ll t = exgcd(a, b, x, y);
if (c%t == 0)//點是否爲整數
printf("%lld %lld\n", -x*c / t, -y*c / t);
else
printf("-1\n");
return 0;
}