The Balance
由题意可得:
{
a
x
1
−
b
y
1
=
d
b
x
2
−
a
y
2
=
d
\begin{cases}ax_1 - by_1 = d \\\\ bx_2 - ay_2 = d\end{cases}
⎩⎪⎨⎪⎧ax1−by1=dbx2−ay2=d
用
e
x
g
c
d
exgcd
exgcd解出两个最小解,然后输出最小的那一对即可。注意如果
x
2
+
y
2
x_2 + y_2
x2+y2更小要先输出
y
2
y_2
y2。
代码
#include<iostream>
#include<cmath>
using namespace std;
void exgcd(int a, int b, int &x, int &y, int &d)
{
if(b == 0) {
x = 1, y = 0;
d = a;
return;
}
exgcd(b, a % b, y, x, d);
y -= a / b * x;
}
void solve(int a, int b, int d, int &x, int &y)
{
int g, t;
exgcd(a,b,x,y,g);
x *= d / g;
t = b / g;
x = (x % t + t) % t; //最小正整数解
y = abs((a * x - d) / b);
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("input.in","r",stdin);
#endif
int a,b,c,x1,x2,y1,y2;
while(cin >> a >> b >> c && (a + b + c)) {
solve(a,b,c,x1,y1);
solve(b,a,c,x2,y2);
if(x1 + y1 < x2 + y2) {
cout << x1 << ' ' << y1 << endl;
}else {
cout << y2 << ' ' << x2 << endl;
}
}
return 0;
}