Topic Links:
http://poj.org/problem?id=2142
Main topic:
There is a balance, there is a quality of a and the weight of B, weight of the number of unlimited and the balance of both ends can be put weights, now required
Punish items with a mass of C on the balance. So the question is: how to place weights so that the number of weights to be placed is as little as possible;
The quantity of weights is the same, the total quality is as low as possible.
Ideas:
Assuming that x weights with a mass of a and y weights of weight B are placed, the title becomes the solution to solve a*x + B*y = c, making
Get |x| + |y| as small as possible, if equal, then a|x| + b|y| as small as possible. Set d = gcd (A, B), first using the extended Euclidean algorithm
a/d*x + b/d*y = C/D Group of Solutions (X0,Y0), then the general solution can be expressed as X = x0 + b/d *t,y = y0-a/d *t.
|x| + |y| = |x0 + b/d*t| + |y0-a/d*t|. This is a piecewise function,|x0 + b/d*t| monotonically increment,|y0-a/d*t| First Order
The adjustment decreases and then increases monotonically. Set a > B, then the slope A/d > b/d, then |x| + |y| = |x0 + b/d*t| + |y0-a/d*t| Total Letter
The number is monotonically decreasing at T < y0*d/a, and monotonically increasing when T >= y0*d/a. So |x| + |y| in t = y0*d/a to get the most
Small value because x, y are integers, T is also an integer, so the minimum value is rounded up and down in T, then the size of the two can be compared.
Results.
There is one more way, though ... But I still don't understand. Hope the great God help answer.
Set D = gcd (A, B), first using the extended Euclidean algorithm a/d*x + b/d*y = C/D Group of Solutions (X0,Y0) , get the basis
equation, because both A and B are positive integers, the as Fruit X is a positive number, then Y is 0 or negative, and y is a positive number if X is 0 negative. First Ask
The minimum value of the |x| X1 is (x0%b + b)% B, come to |y| value Y1 = | (C/D-a/d*x0)/(B/D) |. In the same vein, you can find |y| of the most
The small value y2 is (y0%a + a)% A, and then the value of |x| x2 = | (C/D-B/d*y0)/(A/D) |. Compare the values of x1+y1 and x2+y2, smaller
Is |x|. + |y| minimum result. It's not clear why AC is possible. Reference post: http://blog.csdn.net/wmn_wmn/article/details/7800547
AC Code:
#include <iostream> #include <algorithm> #include <cstdio> #include <cstring> #define LL __ Int64using namespace Std; ll GCD (ll A,ll b) {if (b = = 0) return A; Return GCD (b,a%b);} void Exgcd (LL a,ll b,ll &d,ll &x,ll &y) {if (!b) {x = 1; y = 0; D = A; } else {EXGCD (b,a%b,d,y,x); Y-= x* (A/b); }}int Main () {LL a,b,c,temp,d; while (Cin >> a >> b >> c && (a| | b| | c) {LL x0,y0; LL GCD = gcd (A, b); a/= gcd; b/= gcd; C/= GCD; EXGCD (A,B,D,X0,Y0); LL X,y,x1,y1,x2,y2; x1 = x0*c; X1 = (x1%b + b)%b; Y1 = (c-a*x1)/b; if (Y1 < 0) y1 =-y1; y2 = y0*c; y2 = (y2%a + a)%a; x2 = (c-b*y2)/A; if (x2 < 0) x2 =-x2; if (X1+y1 < x2+y2) x = x1,y = y1; else x = X2,y = y2; cout << x << ' << y << Endl; } return 0;}
POJ2142 the Balance "two yuan one-time equation"