【扩展欧几里得】——poj2142——分析单调性以优化问题

                                                The Balance

Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 5702   Accepted: 2469

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,现在要称出重量为c的物品,问你至少需要多少a和b,

答案需要满足a的数量加上b的数量和最小,并且他们的重量和也要最小。(两个盘都可以放砝码)

 

分析:

假设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附近了,

在t点左右5个点的范围内取最小的即可

 

代码如下:

#include<iostream>
#include<stdio.h>
#include<algorithm>
using namespace std;

int a,b,c;

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

int main()
{
    int d,x,y;
    while(cin>>a>>b>>c,(a+b+c))
    {
        int flag=0;
        if(a<b)
        {
            flag=1;
            swap(a,b);
        }
        d=exgcd(a,b,x,y);

        //求通解
        x=x*c/d;
        y=y*c/d;

        //求最低点
        int t=y*d/a;
        int ans=0xfffffff;

        int x1,y1,x2,y2;
        for(int i=t-5;i<=t+5;i++)
        {
            x2=x+(b/d)*i;
            y2=y-(a/d)*i;
            if(abs(x2)+abs(y2)<ans)
            {
                ans=abs(x2)+abs(y2);
                x1=x2;
                y1=y2;
            }
        }
        if(flag==0)
            printf("%d %d\n",abs(x1),abs(y1));
        else
            printf("%d %d\n",abs(y1),abs(x1));
    }
    return 0;
}

 

posted @ 2016-07-13 11:34  琥珀川||雨露晨曦  阅读(116)  评论(0)    收藏  举报