poj-2142-

The Balance
Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 7092   Accepted: 3115

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

Japan 2004

题意:用

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

方法

假设 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附近了,只要在附近枚举几个值就能找到最优解了。

扫描二维码关注公众号,回复: 3921848 查看本文章
#include <iostream>
#include <math.h>
using namespace std;

int exgcd(int a, int b, int &x, int &y)//模板,a*x+b*y=c

{
    if(b == 0)
    {
        x = 1, y=0;
        return a;
    }
    int ans = exgcd(b,a%b,x,y);
    int temp = x;
    x = y;
    y = temp-a/b*y;
    return ans;
}
int main()
{
    int a, b, c, x, y, x1, x2, y1, y2;
    bool flag;
    while(cin >> a >> b >> c && (a||b||c))//运用x=x0+b/c*t,y=y0-a/c*t,且保证a>b,(t为一个变量)
    {
        flag = 0;
        if(a<b)
        {
            swap(a,b);
            flag = 1;//flag记录a,b是否交换,用于输出判断
        }
        int t = exgcd(a,b,x,y);
        a /= t, b /= t, c /= t;
        x = c*x, y = c*y;//使之转换为x*a+y*b=1,此时c=1
        int r = y/a, rx, ry;//r = t
        while(y-a*r<0)//找到使y-a*r/c<0的最大r
            r--;
        x1 = abs(x+b*r), y1 = abs(y-a*r);
        r++;//找的使y-a*r/c>=0的最小r
        x2 = abs(x+b*r), y2 = abs(y-a*r);
        if((x1+y1<x2+y2) || ((x1+y1==x2+y2) && (x1*a+y1*b<x2*a+y2*b)))//在y-a*r/c=0附近两点找更小的x+y
            rx = x1, ry = y1;
        else
            rx = x2, ry = y2;
        if(flag)
            cout << ry << " " << rx << endl;
        else
            cout << rx << " " << ry << endl;
    }
    return 0;
}


猜你喜欢

转载自blog.csdn.net/Accept1234/article/details/76037275