POJ 2142 The Balance(扩展欧几里德解方程)

POJ 2142 The Balance(扩展欧几里德解方程)

The Balance
Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 2490   Accepted: 1091

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.



POJ 2142 The Balance(扩展欧几里德解方程)

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

 
 
 
代码:
#include<stdio.h>
#include<math.h>
#include<iostream>
#include<algorithm>
using namespace std;
int extend_gcd(int a,int b,int &x,int &y)
{
    int m,tmp;
    if(b==0&&a==0) return -1;
    if(b==0)
    {
        x=1;
        y=0;
        return a;
    }    
    m=extend_gcd(b,a%b,x,y);
    tmp=x;
    x=y;
    y=tmp-(a/b)*y;
    return m;
}    
int main()
{
    
    int a,b,d;
    int x,y;
    int X,Y;
    int X1,Y1;
    while(scanf("%d%d%d",&a,&b,&d))
    {
        if(a==0&&b==0&&d==0) break;
        int flag=0;
        if(a<b)
        {
            flag=1;
            int t=a;
            a=b;
            b=t;
        }    
        int gcd=extend_gcd(a,b,x,y);
        x*=d/gcd;
        y*=d/gcd;
        
        int tmp=a/gcd;//Y=y-tmp*t;
        double t=(double)y/tmp;
        int t1=(int)floor(t);
        
        X=x+(b/gcd)*t1;
        Y=y-tmp*t1;
        if(t1!=t)//t是小数
        {
            t1=t1+1;
            X1=x+(b/gcd)*t1;
            Y1=y-tmp*t1;
            if(abs(X1)+abs(Y1)<abs(X)+abs(Y))
            {
                X=X1;
                Y=Y1;
            } 
            else if(abs(X1)+abs(Y1)==abs(X)+abs(Y) && a*abs(X1)+b*abs(Y1)<a*abs(X)+b*abs(Y))
            {
                X=X1;
                Y=Y1;
            }    
        }       
        if(flag==0)
            printf("%d %d\n",abs(X),abs(Y));
        else
             printf("%d %d\n",abs(Y),abs(X));
    }   
    return 0; 
}    

 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/110424.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 群、环、域的概念总结[通俗易懂]

    群、环、域的概念总结[通俗易懂]很容易看懂群简而言之,群的概念可以理解为:一个集合以及定义在这个集合上的二元运算,满足群的四条公理,封闭性、结合性、单位元、反元素。具体理解为:封闭性:在集合上作任意二元运算,不会诞生新的运算,这个集合已经经过充分的完美拓扑。结合性:组合一个二元操作链,之间没有先后运算的区别,这种操作是平坦的(区别交换律)。单位元:具有单位的属性,单位元和任何一个元素操作等于那个元素本身。…

    2022年6月18日
    59
  • unittest测试框架原理_学软件测试4个月没找到工作

    unittest测试框架原理_学软件测试4个月没找到工作unittest框架解析unittest是python的单元测试框架,unittest单元测试提供了创建测试用例,测试套件以及批量执行的方案,unittest在安装pyhton以后就直接自带了,直接importunittest就可以使用。作为单元测试的框架,unittest也是可以对程序最小模块的一种敏捷化的测试。在自动化测试中,必须需要知道所使用语言的单元测试框架。利用单元测试框架,创建一个类,该类继承unittest的TestCase,这样可以把每个case看成是一个最小的单元,

    2022年10月15日
    2
  • win+r常用指令怎么打开_R语言指令

    win+r常用指令怎么打开_R语言指令最近在学习Linux,被命令行深深吸引了,陷入其中不能自拔,考虑到Windows上也有cmd命令行,但对新人来说不是很友好。这次我们就先讲一下Win+R运行框里的快捷键,绝对能提高不少效率!什么是Win+R防止有些小白看不懂,所以说明一些,使用Windows+R快捷键就可以打开如下图的运行窗口,在里面输入命令可以方便快捷地打开很多东西,而且本文的所有操作都是在这个运行框里输入的,不要与cm…

    2022年10月12日
    4
  • 在报关的过程中会不会出现两个商检

    在报关的过程中会不会出现两个商检问题:1、我刚接触报关,我想知道在报检后如果检验检疫局要商检,那么在接下来的报关过程中我们还会再要商检吗?2、还有我想知道法检是指哪些检验,三检和法检有什么区别,我知道三检包括商检那么法检报不包括呢?回答:1.只有报检手续都办好了,出具通关单后,凭通关单才可以报关。报关过程和商检已经没有关系了。报关中不存在两个商检。但是商检整个流程会分别在报关前后完成。所以,可能让你混淆以为是两个商检。

    2025年12月3日
    4
  • 看板娘代码

    看板娘代码大部分摘自:https://www.cnblogs.com/hean/p/11167216.html需要三个文件和一个可选文件waifu.css(看板娘在页面的位置以及大小)waifu-tips.js(看板娘的语言设置)live2d.min.js(一些点击之后的动作)flat-ui.min.css(看板娘的选项PS:右面的选项,不需要可以不配置)链接:https://…

    2025年5月24日
    4
  • 数据库三范式

    数据库三范式

    2021年5月11日
    146

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号