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

题目链接:This is the link

The Balance

Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 8782   Accepted: 3815

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,找到最小的(|x|+|y|)最小&&(|x|*a+|y|*b)最小的(x,y)组合

题目一眼就看出是使用扩展欧几里得算法,求解,但是却要找到适合题目的解,

具体思路:

使用扩展欧几里得算法,求出a*x+b*y=GCD(a,b)的解,

然后求解线性同余方程a*x+b*y=c,令d=GCD(a,b)

求得: \\ x_0=\frac{x*c}{d} \ \ \ \ \ \ \ \ \ \ x=x_0+b*t \\ \\ y_0=\frac{y*c}{d} \ \ \ \ \ \ \ \ \ \ y=y_0-a*t \\ 

那么对于 |x|+|y|==|x_0+b*t|+|y_0-a*t |,我们规定 a>b(如果 a<b,我们便交换 a b),从这个式子中, 我们可以轻易的发现:|x_0+b*t|是单调递增的,|y_0-a*t|是单调递减的,而由于我们规定了 a>b, 那么减的斜率边要大于增的斜率,于是整个函数减少的要比增加的快,但是由于绝对值的符号的作用,最终函数还是递增的。 也就是说,函数是凹的,先减小,再增大。那么什么时候最小呢?很显然是 y_0-a/d*t==0的时候, 于是我们的最小值|x|+|y|也一定是在 t=y*d/a 附近了,只要在附近枚举几个值就能找到最优解了。

具体解析在 代码中

This is the code

#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<iostream>
#include <iomanip>
#include<list>
#include<map>
#include<queue>
#include<sstream>
#include<stack>
#include<string>
#include<set>
#include<vector>
using namespace std;
#define PI acos(-1.0)
#define EPS 1e-8
#define MOD 1e9+7
#define LL long long
#define ULL unsigned long long     //1844674407370955161
#define INT_INF 0x7f7f7f7f      //2139062143
#define LL_INF 0x7f7f7f7f7f7f7f7f //9187201950435737471
const int dr[]={0, 0, -1, 1, -1, -1, 1, 1};
const int dc[]={-1, 1, 0, 0, -1, 1, -1, 1};
// ios.sync_with_stdio(false);
// 那么cin, 就不能跟C的 scanf,sscanf, getchar, fgets之类的一起使用了。
int extended_gcd(int a,int b,int &x,int &y)
{
    if(!b)
    {
        x=1;
        y=0;
        return a;
    }
    int gcd=extended_gcd(b,a%b,y,x);
    y-=a/b*x;
    return gcd;//返回最大公约数
}
int main()
{
    int a,b,c;
    while(scanf("%d%d%d",&a,&b,&c)!=EOF&&a&&b&&c)
    {
        bool falg=false;
        if(a<b)
        {
            swap(a,b);
            falg=true;
        }
        int x,y;
        int gcd=extended_gcd(a,b,x,y);
        //简化运算
        a/=gcd;b/=gcd;c/=gcd;
        //计算线性方程的解
        x*=c;y*=c;
        //(实际上是:t=y/(a/gcd)//这里的a是最开始的a),因为之前简化的运算,直接t=y/a,下面一些地方的处理类似
        int t=y/a;
        //寻找接近题意要求的t
        while(y-a*t<0)
            t--;
        //题目要求都是对绝对值说的
        int x1,x2,y1,y2;
        x1=fabs(x+b*t),y1=fabs(y-a*t);
        t++;
        x2=fabs(x+b*t),y2=fabs(y-a*t);
        //下面的一部是按照题目要求寻找最优解
        if(x1+y1<x2+y2||((x1+y1==x2+y2)&&(x1*a+y1*b<x2*a+y2*b)))
            x=x1,y=y1;
        else
            x=x2,y=y2;
        if(falg)//按照原来的顺序输出
            printf("%d %d\n",y,x);
        else
            printf("%d %d\n",x,y);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/sdau_fangshifeng/article/details/81585181