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)
上一篇 2021年8月18日 上午6:00
下一篇 2021年8月18日 上午7:00


相关推荐

  • java轻量级web框架_什么是框架

    java轻量级web框架_什么是框架JEMSF框架前言今天我们准备向广大开发人员推荐一种新的框架,暂时取名JEMSF,如果您已经对Struts、Tapestry以及Spring和Hibernat有一些了解,那么应该可以更好的理解下面的文章,JEMSF是我在工作生涯中最大的一个创造,经历了很多考验和应用的试验,最后形成JEMSF。序一种新的事物的诞生需要经历很多的考验,我自认为JEMSF是一个很好的WEB应用框架,很久以前(2…

    2025年9月30日
    5
  • AUC计算公式及python代码

    AUC计算公式及python代码前言 auc 的主要用于分析二分类的准确率 并且主要是对回归任务做分析 当然分类也阔以 计算原理一个原理是 roc 曲线的面积 但是比较复杂 代码不好实现所以基本上不会那么写 还一个是基于 rank 的公式计算 原理比较复杂 本文介绍最简单的实现 计算原理 遍历正负样本对正样本的概率大于负样本 auc 1 正样本的概率等于负样本 auc 0 5 正样本的概率小于负样本 auc 0 遍历完毕 auc auc 正负样本对数举个例子 label 1 0 0 pre

    2026年3月26日
    2
  • React爬坑之路三:Dva

    React爬坑之路三:Dva前两篇写了 react 各种基本操作和主要概念但其实没必要那么复杂直接用框架就好啦这年头前端框架真是一睁眼就多出好几个傻瓜级教程写的不合理的地方请批评指正 React 官网 https reactjs org 菜鸟教程 http www runoob com react react tutorial htmlES6 入门 http es6 ruanyifeng com AntDesign https ant design index cnRedux https www red

    2026年3月18日
    2
  • 图形推理题 答案揭晓图_图形推理1000题

    图形推理题 答案揭晓图_图形推理1000题本周一,小灰给大家出了一道图形推理题,有许多小伙伴都在踊跃作答,非常感谢大家的积极参与!原题如下,需要根据前两行的图形规律,找出第三行最后一个图形是什么:这道题的难度不小…

    2025年10月12日
    5
  • javascript获取url参数_正在获取网络参数一直不出来

    javascript获取url参数_正在获取网络参数一直不出来在做项目过程中,经常会遇到获取URL参数的问题。每次都是百度一下,找许久才找到能用的方法。今天我自己琢磨了一下,写了一个方法,实测有用。我有一个链接http://www.myrealmname.

    2022年8月6日
    8
  • VMWARE虚拟机上网三种方法分析

    VMWARE虚拟机上网三种方法分析VMWARE虚拟机不能上网三种方法分析  PostBy:2009-1-2320:43:00 bridged(桥接模式)、NAT(网络地址转换模式)host-only(主机模式)。理论理解:1.bridged(桥接模式)在这处模式下,虚拟机等同于网络内的一台物理主机,可对手动设置IP,子网掩码,DNS,且IP地址要和主机的IP在同一网段内。这样,虚拟机就和主

    2022年5月19日
    39

发表回复

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

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