poj 2142 The Balance (扩展欧几里得算法)

The Balance
Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 3113  

Accepted: 1370

链接:http://poj.org/problem?id=2142

Description

Ms. Iyo Kiffa-Australis has a balance and only two kinds of weights to measure a dose of medicine. For example, to measure 200mg of aspirin using 300mg weights and 700mg weights, she can put one 700mg weight on the side of the medicine and three 300mg weights on the opposite side (Figure 1). Although she could put four 300mg weights on the medicine side and two 700mg weights on the other (Figure 2), she would 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: a != 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 should be composed of lines, each corresponding to an input dataset (a, b, d). An output line should contain two nonnegative integers x and y separated by a space. They should satisfy the following three conditions.
  • You can measure dmg using x many amg weights and y many 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) should appear in the output.

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



题目大意:
此题就是给定三个数 a, b, d, 计算最少的 a, b 的组合得到 d, 就是 n*a + m*b = d, 求 m+n 最小。

解题思路:
(扩展欧几里得算法):
      

   扩展欧几里德算法是用来在已知a, b求解一组x,y使得  a*x+b*y=Gcd(a,b) (解一定存在,根据数论中的相关定理)。 扩展欧几里德常用在求解模线性方程及方程组中。

      下面是一个使用C++的实现:

    

 1 int exgcd(int a, int b, int &x, int &y)
 2 {
 3     if(b == 0) {
 4         x = 1, y = 0;
 5         return a;
 6     }
 7     int d = exgcd(b, a%b, x, y);
 8     int t = x;
 9     x = y;
10     y = t - a/b*y;
11     return d;
12 }

把这个实现和Gcd的递归实现相比,发现多了下面的x,y赋值过程,这就是扩展欧几里德算法的精髓。

  可以这样思考:

  对于a' = b,  b' = a % b 而言,我们求得 x, y使得 a'x + b'y = Gcd(a', b')

  由于b' = a % b = a - a / b * b  (注:这里的/是程序设计语言中的除法)

  那么可以得到:

    a'x + b'y = Gcd(a', b') ==> bx + (a - a / b * b)y = Gcd(a', b') = Gcd(a, b) ==> ay +b(x - a / b*y) = Gcd(a, b)

  因此对于a和b而言,他们的相对应的p,q分别是 y和(x-a/b*y).

   不定方程的求解全过程,步骤如下:

  求a * x + b * y = n的整数解。

  1、先计算Gcd(a,b),若n不能被Gcd(a,b)整除,则方程无整数解;否则,在方程两边同时除以Gcd(a,b),

    得到新的不定方程a' * x + b' * y = n',此时Gcd(a',b')=1;

      2、利用欧几里德算法求出方程a' * x + b' * y = 1 的一组整数解x0, y0,则n' * x0,  n'* y0 是方程a' * x + b'* y  = n'的一组整数解;

  3、根据数论中的相关定理,可得方程a' * x + b' * y = n'的所有整数解为:

         x = n' * x0 + b' * t  ;

        y = n' * y0 - a' * t  (t为整数)

    上面的解也就是a * x + b * y = n 的全部整数解。

此题的解在 y=a*t 附近,所以要附加 循环查找部分:
while(y - a*t < 0)
            t--;
AC代码:
 1 #include<iostream>
 2 
 3 using namespace std;
 4 
 5 int exgcd(int a, int b, int &x, int &y)
 6 {
 7     if(b == 0) {
 8         x = 1, y = 0;
 9         return a;
10     }
11     int d = exgcd(b, a%b, x, y);
12     int t = x;
13     x = y;
14     y = t - a/b*y;
15     return d;
16 }
17 
18 int absv(int n)
19 {
20     return (n < 0 ? -n : n);
21 }
22 
23 int main()
24 {
25     int a, b, d, x , y , r, k, t;
26     while(cin >> a >> b >> d && a)
27     {
28         if(a < b) {
29             swap(a, b);         //转换函数,使得 a>b
30             k = 1;
31         }
32         else
33             k = 0;
34 
35         x = 0, y = 0;
36         r = exgcd(a, b, x, y);
37         d /= r, a /= r, b /= r;
38         x *= d, y *= d;
39         t = y / a;
40         while(y - a*t < 0)
41             t--;
42 
43         int x1, x2, y1, y2;
44         x1 = absv(x + b*t), y1 = absv(y - a*t);
45         t = t++;
46         x2 = absv(x + b*t), y2 = absv(y - a*t);
47 
48         // 判断x1+y1 与 x2+y2的大小,相等时还要比较x1*a+y1*b 与 x2*a+y2*b的大小,才能得到最优解
49         if((x1+y1 > x2+y2) ||((x1+y1 == x2+y2) && (x1*a+y1*b > x2*a+y2*b)))
50             x = x2, y = y2;
51         else
52             x = x1, y = y1;
53 
54         // 输出与 a 和 b 的大小有关;
55         if(k)
56             cout << y << ' ' << x << endl;
57         else
58             cout << x << ' ' << y << endl;
59     }
60     return 0;
61 }

代码一定要自己敲,理解了才是自己的!
原文地址:https://www.cnblogs.com/Duahanlang/p/3067405.html