Face questions
【Problem Description】
You have a balance (balance of the left and right sides can be put weight) the weight of \ (A, B \) ( \ (. 1 \ Le A, B \ 10000 Le \) ) Two weight. Let you find out the weight of a scheme called \ (c \) ( \ (1 \ Le c \ Le 50000 \) ) items, if a variety of programs, please output two kinds of weight requires a minimum sum of the number of Program.
[Input Format]
There are several lines of three numbers, \ (A, B, C \) .
Represented by 000 at the end.
[Output format]
Several lines of two numbers, representing each interrogation \ (A \) number and \ (B \) number
If no solution output no solution
[Sample input]
700 300 200
500 200 300
500 200 500
275 110 330
275 110 385
648 375 4002
3 1 10000
0 0 0
[Sample Output]
1 3
1 1
1 0
0 3
1 1
49 74
3333 1
solution
Containing a maximum value of the absolute value function parameters.
program
#include <iostream>
#include <vector>
using namespace std;
typedef long long ll;
#define INF 0x3f3f3f3f
ll exgcd(ll a, ll b, ll& x, ll& y) {
if (b) {
ll xp, yp;
ll d = exgcd(b, a % b, xp, yp);
x = yp; y = xp - (a / b) * yp;
return d;
} else {
x = 1; y = 0;
return a;
}
}
ll f(ll k, ll x, ll b, ll d, ll y, ll a) {
return abs(x + b * k / d) + abs(y - a * k / d);
}
int main() {
ll a, b, c;
while (cin >> a >> b >> c && a && b && c) {
ll d, x, y;
d = exgcd(a, b, x, y);
if (c % d) {
cout << "no solution" << endl;
continue;
}
x *= c / d; y *= c / d;
// assert(a * x + b * y == c);
vector<ll> cand;
cand.push_back(-d * x / b);
cand.push_back(-d * x / b + 1);
cand.push_back(-d * x / b - 1);
cand.push_back(d * y / a);
cand.push_back(d * y / a + 1);
cand.push_back(d * y / a - 1);
ll bval = INF, xf, yf;
for (const int& k : cand) {
if (f(k, x, b, d, y, a) < bval) {
bval = f(k, x, b, d, y, a);
xf = x + b * k / d;
yf = y - a * k / d;
}
}
cout << abs(xf) << " " << abs(yf) << endl;
}
return 0;
}