POJ 2142The Balance(扩展欧几里得)

The Balance

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

Source


题目大意:

此题就是给定三个数 a, b, c, 计算最少的 a, b 的组合得到 c, 就是 x*a + y*b = c, 求 x+y 最小。

解题思路:

(扩展欧几里得算法):   

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

解模线性方程及方程组中。

欧几里得算法代码实现:

int exgcd(int a,int b,int &x,int &y)
{
    if(b==0)
    {
        x=1;y=0;
        return a;
    }
    int d=exgcd(b,a%b,x,y);
    int tmp=x;
    x=y;
    y=tmp-a/b*y;
    return d;
}

把这个实现和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 的全部整数解。


对于此题目
假设 a 砝码我们用了 x 个,b 砝码我们用了 y 个。那么天平平衡时,就应该满足ax+by==c。x,y 为正时表示放在和 c 物品的另一边,为负时表示放在 c 物品的同一边。
于是题意就变成了求|x|+|y|的最小值了。x 和 y 是不定式 ax+by==c 的解。刚刚上面已经提到了关于 x,y 的所以解的同式,即 
x=x0+b/d*t 
y=y0-a/d*t 
穷举所有解,取|x|+|y|最小的?显然是行不通的,仔细分析: 
|x|+|y|==|x0+b/d*t|+|y0-a/d*t|,我们规定 a>b(如果 a<b,我们便交换 a b),从这个式子中,
我们可以轻易的发现:|x0+b/d*t|是单调递增的,|y0-a/d*t|是单调递减的,而由于我们规定了 a>b,
那么减的斜率边要大于增的斜率,于是整个函数减少的要比增加的快,但是由于绝对值的符号的作用,最终函数还是递增的。
也就是说,函数是凹的,先减小,再增大。那么什么时候最小呢?很显然是 y0-a/d*t==0 的时候,
于是我们的最小值|x|+|y|也一定是在 t=y0*d/a附近了,只要在附近枚举几个值就能找到最优解了。

AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#define INF 0x3f3f3f3f
using namespace std;

int exgcd(int a,int b,int &x,int &y)
{
    if(b==0)
    {
        x=1;y=0;
        return a;
    }
    int d=exgcd(b,a%b,x,y);
    int tmp=x;
    x=y;
    y=tmp-a/b*y;
    return d;
}

int main()
{
    int a,b,c,x,y;
    bool flag;
    while(cin>>a>>b>>c&&a|b|c)
    {
        flag=false;
        if(a<b)
        {
            swap(a,b);
            flag=true;
        }
        int r=exgcd(a,b,x,y);
        a/=r;b/=r;c/=r;
        x*=c;y*=c;
        int t=y/a;
        int rx,ry;
        while(y-a*t<0)
            t--;
        int x1,y1,x2,y2;
        x1=abs(x+b*t);y1=abs(y-a*t);
        t++;
        x2=abs(x+b*t);y2=abs(y-a*t);
        if((x1+y1<x2+y2)||((x1+y1==x2+y2)&&(x1*a+y1*b<x2*a+y2*b)))
            rx=x1,ry=y1;
        else
            rx=x2,ry=y2;
        if(flag)
            cout<<ry<<' '<<rx<<endl;
        else
            cout<<rx<<' '<<ry<<endl;
    }
    return 0;
}



  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值