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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 天才就是这样炼成的

    天才就是这样炼成的from 水木社区 天才就是这样炼成的——记菲尔兹奖获得者澳大利亚数学神童、陶哲轩作者:舒锋澳大利亚土生土长的华裔天才陶哲轩(TerrenceTao)于2006年年8月获得数学界的诺贝尔奖–菲尔兹奖(FieldsMedal)。国际数学会(IMU)每年在国际数学大会上颁菲尔兹奖给两至四名数学家,IMU表示,陶教授被颁这个殊荣,是因他对偏微分方程、组合数学、混合分析和堆垒素数论的杰出贡献。陶

    2022年5月8日
    38
  • pycharm安装好了打不开_保留我的文件 删除所有内容

    pycharm安装好了打不开_保留我的文件 删除所有内容昨晚电脑系统崩了》》就直接恢复系统》》保留所有文件今天Pycharm无法打开了,就重新安装了,但是安装后咋也打不开。解决方法:C:\Users\ORANGE(用户名)下删除之前残留的问题件.Pycharm2019.3…

    2022年8月28日
    2
  • pycharm配置Git和Github[通俗易懂]

    pycharm配置Git和Github[通俗易懂](Windows)pycharm配置Git和Github,协同开发1、安装Git1.1、验证是否安装git#cmd命令git–version#显示git版本则证明安装成功1.2、下载gitwindow下载链接安装好git之后,配置环境变量,验证git是否安装成功。1.3、配置git用户名和邮箱gitconfig–globaluser.name用户名gitconfig–globaluser.email邮箱1.4、在pycharm中配置git点击Fil

    2022年8月26日
    10
  • 软件测试常见面试题

    软件测试常见面试题伴随着疫情的好转,又到了一年收获的季节。最近也有一些面试,整理下常用的测试题目,没有标准答案,需要结合自身的工作实践去应答。功能测试相关1、测试流程以及对应阶段的输出有哪些?2、Bug的优先级

    2022年8月6日
    5
  • matlab 实现 garch 模型波动率估计

    matlab 实现 garch 模型波动率估计matlab 实现 garch 模型波动率估计 matlab 实现 garch 模型波动率估计代码高亮问题数据获取数据处理描述性统计时间序列平稳性检验相关和偏自相关 arch 效应检验建立 garch 模型波动率估计代码及文档地址代码高亮问题这边首先说一个问题 你们看到的 matlab 代码没有高亮 看上去很不舒服 但这个锅是 csdn 的 真垃圾 连个语法高亮

    2025年6月10日
    2
  • 论.idea文件夹是干嘛的「建议收藏」

    论.idea文件夹是干嘛的「建议收藏」Problempython为什么每次创建的文件目录下都含.idea/文件夹?该文件夹又是用来干嘛的?Answer当使用pycharm作为IDE时,会自动生成.idea/文件夹来存放项目的配置信息。其中包括版本控制信息、历史记录等等。…

    2022年8月27日
    6

发表回复

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

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