The balance
| Time limit:5000 Ms |
|
Memory limit:65536 K |
| Total submissions:2490 |
|
Accepted:1091 |
Description
Ms. iyo Kiffa-has been has a balance and only two kinds of weights to measure a dose of medicine. for example, to measure 200 mg of aspirin using 300 mg weights and 700 mg weights, she can put one 700 mg weight on the side of the medicine and three 300 mg weights on the opposite side (figure 1 ). although she cocould put four 300 mg weights on the medicine side and two 700 mg weights on the other (Figure 2 ), she wocould not choose this solution because it is less convenient to use more weights.
You are asked to help her by calculating how many weights are required.
Input
The input is a sequence of datasets. a dataset is a line containing three positive integers A, B, and D separated by a space. The following relations hold:! = B, A <= 10000, B <= 10000, and D <= 50000. you may assume that it is possible to measure D Mg using a combination of a mg and B mg weights. in other words, you need not consider "no solution" cases.
The end of the input is indicated by a line containing three zeros separated by a space. It is not a dataset.
Output
The output shoshould be composed of lines, each corresponding to an input dataset (a, B, d ). an output line shoshould contain two nonnegative integers x and y separated by a space. they shoshould satisfy the following three conditions.
- You can measure DMG using X your AMG weights and y your BMG weights.
- The total number of weights (X + Y) is the smallest among those pairs of nonnegative integers satisfying the previous condition.
- The total mass of weights (AX + by) is the smallest among those pairs of nonnegative integers satisfying the previous two conditions.
No extra characters (e.g. Extra spaces) shocould appear in the output.
Sample Input
700 300 200500 200 300500 200 500275 110 330275 110 385648 375 40023 1 100000 0
Sample output
1 31 11 00 31 149 743333 1
Source
Japan 2004
Code :
# Include <stdio. h> # Include <Math. h> # Include <Iostream> # Include <Algorithm> Using Namespace STD; Int Extend_gcd ( Int A, Int B, Int & X, Int & Y ){ Int M, TMP; If (B = 0 & A = 0 ) Return - 1 ; If (B = 0 ) {X = 1 ; Y = 0 ; Return A;} m = Extend_gcd (B, A %B, x, y); TMP = X; x = Y; y = TMP-(A/B )* Y; Return M ;} Int Main (){ Int A, B, d; Int X, Y; Int X, Y; Int X1, Y1; While (Scanf ( " % D " , & A, & B ,& D )){ If (A = 0 & B = 0 & D = 0 ) Break ; Int Flag = 0 ; If (A < B) {flag = 1 ; Int T = A; = B; B = T ;} Int GCD = Extend_gcd (a, B, x, y); x * = D/ GCD; y * = D/ GCD; Int TMP = A/GCD; // Y = Y-TMP * t; Double T = ( Double ) Y/ TMP; Int T1 = ( Int ) Floor (t); x = X + (B/gcd )* T1; y = Y-TMP * T1; If (T1! = T) // T is decimal {T1 = T1 + 1 ; X1 = X + (B/gcd )* T1; Y1 = Y-TMP * T1; If (ABS (X1) + ABS (Y1) <ABS (x) + ABS (y) {x = X1; y = Y1 ;} Else If (ABS (X1) + ABS (Y1) = ABS (x) + ABS (y) & A * ABS (X1) + B * ABS (Y1) <A * ABS (x) + B * ABS (y) {x = X1; y = Y1 ;}} If (Flag = 0 ) Printf ( " % D \ n " , ABS (x), ABS (y )); Else Printf ( " % D \ n " , ABS (Y), ABS (x ));} Return 0 ;}