首页 > ACM题库 > HDU-杭电 > HDU 1356 The Balance-数论-[解题报告] C++
2013
12-09

HDU 1356 The Balance-数论-[解题报告] C++

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.

输入:

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 dmg using a combination of amg and bmg 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.

输出:

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.
  1. You can measure dmg using x many amg weights and y many bmg weights.
  2. The total number of weights (x + y) is the smallest among those pairs of nonnegative
  integers satisfying the previous condition.
  3. 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.

样例输入:

700 300 200
500 200 300
500 200 500
275 110 330
275 110 385
648 375 4002
3 1 10000
0 0 0

样例输出:

1 3
1 1
1 0
0 3
1 1
49 74
3333 1

http://acm.hdu.edu.cn/showproblem.php?pid=1356

这是欧几里得的模板题。。。

  1.  [扩展眼欧几里德]给定 a b d找到满足ax+by=d 的令|x|+|y|最小 
  2. | 参考线性同余方程 g=gcd(a,b) , ax0+by0=gcd(a,b) 
  3. | 程的一组解: x=x0*d/g; y=y0*d/g;    
  4. | 方程的全部解:x..=x+b/g*t   y..=y-a/g*t 
  5. | |x|+|y|= | x + b/g*t | + | y - a/g*t | (这里有绝对值,所以要想总体会小,那么里面的值要尽量小)    
  6. | 这个关于t的函数的最小值应该在|y - a/g*t|为零附近,即t=y*g/a 
  7. | 在 y*g/a 附近的两整数点里取t,再直接验证这两点即可。 
#include<stdio.h>
__int64 x,y;
__int64 fabs(__int64 a)
{
    return a>0?a:-a;
}
__int64 exgcd(__int64 a,__int64 b)
{
    __int64 t,ans;
    if(b==0)
    {
            x=1;
            y=0;
            return a;
    }
    ans=exgcd(b,a%b);
    t=x;
    x=y;
    y=t-a/b*y;
    return ans;
}
int main()
{
    __int64 max,xx,yy,a,b,d,temp,i,flag,aa,bb,t;
    while(scanf("%I64d%I64d%I64d",&a,&b,&d),a||b||d)
    {
         flag=0;
         if(a<b)
         {
             temp=a;
             a=b;
             b=temp;
             flag=1;
         }
         temp=exgcd(a,b);
         x=x*d/temp;
         y=y*d/temp;
         max=9999999;
         t=y*temp/a;
         for(i=t-1;i<=t+1;i++)
         {
             xx=fabs(x+b/temp*i);
             yy=fabs(y-a/temp*i);
             if(xx+yy<max)
             {
                max=xx+yy;
                aa=xx;
                bb=yy;
             }
         } 
         if(flag)
         printf("%I64d %I64d\n",bb,aa);
         else printf("%I64d %I64d\n",aa,bb); 
    }
    return 0;
}

 

 

解题报告转自:http://www.cnblogs.com/huzhenbo113/p/3266710.html


  1. 第2题,TCP不支持多播,多播和广播仅应用于UDP。所以B选项是不对的。第2题,TCP不支持多播,多播和广播仅应用于UDP。所以B选项是不对的。