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. 「CF438D The Child and Sequence」

    一道CF线段树好题. 前置芝士 线段树:一个很有用数据结构. 势能分析:用来证明复杂度,其实不会也没什么关系啦. 具体做法 不难发现,对于一个数膜一个大于它的数后,这个数至少减少一半,每个数最多只能被 ...

  2. linux服务器上安装jdk8的两种方法

    这里介绍两种安装方式: yum安装(力荐) 从官网下载包安装 获得一台linux服务器 要在linux下安装jdk,首先你得先有一台linux服务器,虚拟机或者租一台都可以   yum安装jdk 在l ...

  3. 3 (mysql实战) 事务隔离

    提到事务,你肯定不陌生,和数据库打交道的时候,我们总是会用到事务.最经典的例子就是转账,你要给朋友小王转 100 块钱,而此时你的银行卡只有 100 块钱. 转账过程具体到程序里会有一系列的操作,比如 ...

  4. redis 初识与安装

    一.redis介绍 redis是一个key-value存储系统.和Memcached类似,它支持存储的values类型相对更多,包括字符串.列表.哈希散列表.集合,有序集合. 这些数据类型都支持pus ...

  5. Python输出三位数以内的水仙花数

    num = 100 while num <= 999: a = num % 10 #取个位数 b = num // 10 % 10 #取十位数 c = num // 100 #取百位数 if n ...

  6. js面试代码中的“坑”

    1.typeof 对类型的判断 (function() { return typeof arguments; } )(); 答案:"Object" 解释:arguments是一个伪 ...

  7. 新闻网大数据实时分析可视化系统项目——7、Kafka分布式集群部署

    Kafka是由LinkedIn开发的一个分布式的消息系统,使用Scala编写,它以可水平扩展和高吞吐率而被广泛使用.目前越来越多的开源分布式处理系统如Cloudera.Apache Storm.Spa ...

  8. 不一样的Vue实战3:布局与组件

    不一样的Vue实战3:布局与组件  发表于 2017-06-05 |  分类于 web前端|  |  阅读次数 11534 http://yangyi1024.com/2017/06/05/%E4%B ...

  9. 腾讯2019秋招--小q爬塔(dp)

    小Q爬塔 题目描述: 小Q 正在攀登一座宝塔,这些宝塔很特别.塔总共有 n 层,但是每两层之间的净高却不相同,所以造成了小Q 爬过每层的时间也不同.如果某一层的高度为 x,那么爬过这一层所需的时间也是 ...

  10. PHP获取远程图片

    <?php // // Function: 获取远程图片并把它保存到本地 // // // 确定您有把文件写入本地服务器的权限 // // // 变量说明: // $url 是远程图片的完整UR ...