The Balance
Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 4781   Accepted: 2092

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

题意是给出了a,b,d的重量。问使用a、b怎么测出d的重量,假设是能够测出的前提下。输出|x|+|y|的最小值。

a*x+b*y=d

之后求一下x的最小值时y的值。再求一遍y的最小值时x的值。两两比较即可。

代码:

#include <iostream>
#include <vector>
#include <string>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std; int xx,yy,yue;
int a,b,d;
vector <int> x_value;
vector <int> y_value; void ex_gcd(int a,int b, int &xx,int &yy)
{
if(b==0)
{
xx=1;
yy=0;
yue=a;
}
else
{
ex_gcd(b,a%b,xx,yy); int t=xx;
xx=yy;
yy=t-(a/b)*yy; }
} void cal()
{
int i;
int min=abs(x_value[0])+abs(y_value[0]);
int min_x=abs(x_value[0]),min_y=abs(y_value[0]); for(i=1;i<2;i++)
{
if(abs(x_value[i])+abs(y_value[i])==min)
{
if((abs(x_value[i])*a+abs(y_value[i])*b)<(min_x*a+min_y*b))
{
min_x=abs(x_value[i]);
min_y=abs(y_value[i]);
}
}
if(abs(x_value[i])+abs(y_value[i])<min)
{
min=abs(x_value[i])+abs(y_value[i]);
min_x=abs(x_value[i]);
min_y=abs(y_value[i]);
}
}
cout<<min_x<<" "<<min_y<<endl;
} int main()
{
while(cin>>a>>b>>d)
{
if(a==0 && b==0 && d==0)
break;
ex_gcd(a,b,xx,yy); x_value.clear();
y_value.clear(); xx=xx*(d/yue);
yy=yy*(d/yue); int r=a/yue;
yy=(yy%r+r)%r;
int xx0,yy0=yy;
xx0=(d-yy*b)/a;
x_value.push_back(xx0);
y_value.push_back(yy); r=b/yue;
xx=(xx%r+r)%r;
x_value.push_back(xx);
yy=(d-xx*a)/b;
y_value.push_back(yy);
cal();
}
return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

POJ 2142:The Balance的更多相关文章

  1. POJ.2142 The Balance (拓展欧几里得)

    POJ.2142 The Balance (拓展欧几里得) 题意分析 现有2种质量为a克与b克的砝码,求最少 分别用多少个(同时总质量也最小)砝码,使得能称出c克的物品. 设两种砝码分别有x个与y个, ...

  2. POJ 3321:Apple Tree + HDU 3887:Counting Offspring(DFS序+树状数组)

    http://poj.org/problem?id=3321 http://acm.hdu.edu.cn/showproblem.php?pid=3887 POJ 3321: 题意:给出一棵根节点为1 ...

  3. POJ 3252:Round Numbers

    POJ 3252:Round Numbers Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10099 Accepted: 36 ...

  4. poj 2142 The Balance

    The Balance http://poj.org/problem?id=2142 Time Limit: 5000MS   Memory Limit: 65536K       Descripti ...

  5. POJ 2142 The Balance(exgcd)

    嗯... 题目链接:http://poj.org/problem?id=2142 AC代码: #include<cstdio> #include<iostream> using ...

  6. The Balance POJ 2142 扩展欧几里得

    Description Ms. Iyo Kiffa-Australis has a balance and only two kinds of weights to measure a dose of ...

  7. POJ 1837:Balance 天平DP。。。

    Balance Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 11878   Accepted: 7417 Descript ...

  8. POJ 2142 The Balance【扩展欧几里德】

    题意:有两种类型的砝码,每种的砝码质量a和b给你,现在要求称出质量为c的物品,要求a的数量x和b的数量y最小,以及x+y的值最小. 用扩展欧几里德求ax+by=c,求出ax+by=1的一组通解,求出当 ...

  9. POJ 2142 The Balance (解不定方程,找最小值)

    这题实际解不定方程:ax+by=c只不过题目要求我们解出的x和y 满足|x|+|y|最小,当|x|+|y|相同时,满足|ax|+|by|最小.首先用扩展欧几里德,很容易得出x和y的解.一开始不妨令a& ...

随机推荐

  1. Pyhton语言的优缺点

    python作为一门高级编程语言,它的诞生虽然很偶然,但是它得到程序员的喜爱却是必然之路. 龟叔给Python的定位是“优雅”.“明确”.“简单”,所以Python程序看上去总是简单易懂,初学者学Py ...

  2. 【C++初学者自学笔记一】(把自己刚学到的东西做一下总结,如果有哪些地方不足请留言指正)

    这是我写的第一个博客关于C++的一些笔记,我不会写的太深奥,因为这样很多人会看不懂(我刚开始学C语言深受其害).个人觉得C++这门语言有些类似于C语言但是有些函数的用法还是有不一样的.C语言中的头文件 ...

  3. 如何优雅地根治null值引起的Bug!

    本人免费整理了Java高级资料,涵盖了Java.Redis.MongoDB.MySQL.Zookeeper.Spring Cloud.Dubbo高并发分布式等教程,一共30G,需要自己领取.传送门:h ...

  4. 源码安装openldap(转)

    Ubuntu安装OpenLDAP(附错误的详细解决办法) 1 下载OpenLDAP源码 http://www.openldap.org/software/download/ 或者 ftp://ftp. ...

  5. Java并发编程:Java内存模型JMM

    简介 Java内存模型英文叫做(Java Memory Model),简称为JMM.Java虚拟机规范试图定义一种Java内存模型来屏蔽掉各种硬件和系统的内存访问差异,实现平台无关性. CPU和缓存一 ...

  6. 「快学springboot」16.让swagger帮忙写接口文档

    swagger简介 官方的介绍 THE WORLD'S MOST POPULAR API TOOLING Swagger is the world's largest framework of API ...

  7. KVM虚拟化与容器的区别理解

    1.KVM虚拟化是linux内核的虚拟化,提供了内核级别的虚拟进程管理,客户空间的程序QEMU-KVM可以提供资源清单和模拟设备,与KVM交互 QEMU-KVM--可以在宿主机器,建立网络(网桥交换机 ...

  8. 吴裕雄 Bootstrap 前端框架开发——Bootstrap 表单:表单帮助文本

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  9. Screen命令让Linux shell在后台运行

    #screen ping ychack.com //挂置后台ping本站 #screen ping baidu.com //挂置后台ping百度 #screen -ls //列出进程 #screen ...

  10. 夯实Java基础(十五)——Java中Comparable和Comparator

    1.前言 对于Java中的对象,我们只能使用基本运算符==.!=来判断一下地址是否相等,不能使用>.<来比较大小.但是在实际的开发中,我们需要对对象进行排序,也就是比较大小,那么应该如何实 ...