POJ - 2142 The Balance

题目:
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

代码如下:

#include<iostream>
#include<cstdio>
using namespace std;
typedef long long ll;
ll ans;
ll gcd(ll a,ll b){return b == 0 ? a : gcd(b,a % b);}
ll exgcd(ll a,ll b,ll &x,ll &y)
{
    if(!b){
        x = 1;
        y = 0;
        return a;
    }
    ans = exgcd(b,a % b,x,y);
    ll temp = x;
    x = y;
    y = temp - (a / b) * y;
    return ans;
}
int main()
{
    ll a,b,d,x,y,x2,y2;
    while(~scanf("%lld%lld%lld",&a,&b,&d) && (a + b + d)){
        ll res = gcd(a,b);
        a /= res;
        b /= res;
        d /= res;//先把式子化为最简式
        exgcd(a,b,x,y);
        x2 = x * d;
        x2 = (x2 % b + b) % b;
        y2 = (d - a * x2) / b;
        if(y2 < 0) y2 = -y2;
        y = y * d;
        y = (y % a + a) % a;
        x = (d - b * y) / a;
        if(x < 0) x = -x;
        if(x + y > x2 + y2) x = x2,y = y2;
        cout << x << " " << y << endl;
    }
    return 0;
}

题意:
告诉砝码a和b的质量,告诉你物体d的质量,让你求最少摆放砝码的个数,相同个数的情况下选取总质量少的那个。
思路:
很容易得出方程ax + by = d;我们要求|x| + |y|的最小值。
首先我们需要把式子化简为最简式(之前没有化简做出来的不是最小值)
ax + by = d ==> ax / gcd(a,b) + by / gcd(a,b) = d / gcd(a,b)
化简完成后,我们就要使用扩展欧几里得算法来求出一组可行解,这里需要分成两类来讨论:
第一类就是砝码a放在左端时的情况:现在我们用扩展欧几里得算出来的x只是ax + by = 1时候的解
现在我们要求ax + by = d时x的解,我们应该让x * d,然后再对b取模得到最小的正整数解x
然后我们把x反代回原方程ax + by = d,在算出y的解,这里计算出y肯定小于0的,这是为何呢?
ax + by = d,当a,b,d都是正数,算出来的x也为正数时,等式要成立y肯定为负数,所以要取反,这里为负数代表b砝码放在右端。
第二类就是考虑b砝码放在左端的情况,这时候x为负数,y为正数。

最后算出两类之后比较哪种情况的质量小,输出就可以了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值