The Balance
Time Limit: 5000MS   Memory Limit: 65536K
     

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

Source

 
题意:
在天平两侧放若干砝码 和一堆沙子,使天平平衡
即求ax + by = c 的解
若x ,y 为负,表示与沙子放在同一侧,为正表示放在另一侧
题目同时要求 |x|+|y| 最小,在满足其最小的情况下 使 a|x|+b|y|最小
可以先求 使|x|+|y| 最小,在其中找 a|x|+b|y|最小
如何求 |x|+|y|最小?
由 扩展欧几里得算法 可以求出 一组通解(x0,y0)
令 d=gcd(a,b)
那么 x= x0 + t*b/d    ,   y=y0 + t*a/d
|x|+|y| = | x0+t*b/d | + | y0-t*a/d |
我们假设 a>b,若输入中a<b,交换
| x0+t*b/d |   单调递增
| y0- t*a/d |   先减后增 
因为 a>b , 所以减的斜率>增的斜率
所以正个函数先减后增
所以 函数必有零点
所以什么时候函数最小?
y0-t*a/d=0时最小
此时 t=y0*d/a
由于取整存在的误差,使函数最小t在t-1到t+1之间
不放心的话 可以扩大范围 t-5到t+5 等等都是没有问题的
#include<cstdio>
#include<algorithm>
using namespace std;
int exgcd(int a,int b,int &x,int &y)
{
if(!b) {x=; y=; return a;}
int d=exgcd(b,a%b,x,y);
int t=x; x=y; y=t-a/b*y;
return d;
}
int main()
{
int a,b,c;
while(scanf("%d%d%d",&a,&b,&c)!=EOF)
{
if(!a&&!b&&!c) return ;
bool f=false;
if(a<b) swap(a,b),f=true;
int x,y;
int d=exgcd(a,b,x,y);
int x0=c/d*x,y0=c/d*y;
int t=d*y0/a;
a/=d; b/=d;
int add=0x7fffffff,mul=0x7fffffff;
int ansx,ansy;
for(int i=t-;i<=t+;i++)
{
x=x0+i*b; y=y0-i*a;
if(x<) x=-x;
if(y<) y=-y;
if(x+y<add)
{
add=x+y;
ansx=x;ansy=y;
mul=a*x+b*y;
}
else if(x+y==add)
{
if(a*x+b*y<mul)
{
ansx=x;ansy=y;
mul=a*x+b*y;
}
}
}
if(!f) printf("%d %d\n",ansx,ansy);
else printf("%d %d\n",ansy,ansx);
}
}

poj 2142 The Balance的更多相关文章

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

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

  2. POJ 2142 The Balance(exgcd)

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

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

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

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

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

  5. POJ 2142 The balance | EXGCD

    题目: 求ax+by=c的一组解,使得abs(x)+abs(y)尽量小,满足前面前提下abs(ax)+abs(by)尽量小 题解: exgcd之后,分别求出让x尽量小和y尽量小的解,取min即可 #i ...

  6. POJ - 2142 The Balance(扩展欧几里得求解不定方程)

    d.用2种砝码,质量分别为a和b,称出质量为d的物品.求所用的砝码总数量最小(x+y最小),并且总质量最小(ax+by最小). s.扩展欧几里得求解不定方程. 设ax+by=d. 题意说不定方程一定有 ...

  7. POJ 2142 - The Balance [ 扩展欧几里得 ]

    题意: 给定 a b n找到满足ax+by=n 的x,y 令|x|+|y|最小(等时令a|x|+b|y|最小) 分析: 算法一定是扩展欧几里得. 最小的时候一定是 x 是最小正值 或者 y 是最小正值 ...

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

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

  9. POJ 2142:The Balance

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

随机推荐

  1. [hihoCoder] 高斯消元·一 [TPLY]

    高斯消元一 题目链接 : http://hihocoder.com/problemset/problem/1195?sid=1269842 很"好aoaoaoaoaoaoa"的高斯 ...

  2. 【BZOJ2333】棘手的操作(左偏树,STL)

    [BZOJ2333]棘手的操作(左偏树,STL) 题面 BZOJ上看把... 题解 正如这题的题号 我只能\(2333\) 神TM棘手的题目... 前面的单点/联通块操作 很显然是一个左偏树+标记 ( ...

  3. 【NOIP2012】疫情控制(二分,倍增,贪心)

    洛谷上的题目链接,题目不在赘述 题解 既然要时间最短,首先考虑二分. 因此,考虑二分时间,问题转换为如何检查能否到达. 如果一支军队一直向上走,能够到达根节点,那么他可以通过根节点到达其他的节点,因此 ...

  4. javascript ES5、ES6的一些知识

    ES6 标签(空格分隔): ES6 严格模式 "use strict" 注意:严格模式也有作用域,如果在某个函数内部声明的话,只在该函数内部有作用 1) 严格模式下全局变量声明必须 ...

  5. Red Hat Enterprise Linux7 配置Tomcat

    笔者是Java前端的一个萌新,电脑刚刚经历了一番脱胎换骨,然后重新装了Win10Pro,所有的开发工具都要重新安装,纠结了一番以后决定还是把一些开发工具从Windows上转移到Linux上,首先考虑了 ...

  6. static与final的区别

    final被修饰的变量为常量一旦赋值不能修改,被修改的方法为最终方法不能被重写,被修饰的类是最终类,不能被继承static被修饰的变量和方法,为该整个类及其类的对象所共享,一个类或对象修改了被定义的类 ...

  7. 世界上最好的语言搭建短链接及统计功能(附API代码)

    前言 在这个营销的时代,短链接和二维码是企业进行营销中非常重要的工具,不仅仅是缩短了链接,而且还可以通过扩展获得更多的数据,诸如点击数.下载量.来源以及时间等等. 网上搜寻了一下比较有名有U.NU和0 ...

  8. 听说你买了 EOS ,连代码什么样都不知道?

    最近发现很多人投资了 EOS,却并不关心 EOS 目前的开发进度和技术细节,如果你投资了 EOS, 还有一定的技术基础,那就更应该关心 EOS 的开发情况了,下面我们就从 EOS 的源代码说起:   ...

  9. Mycat 分片规则详解--一致性hash分片

    实现方式:基于hash算法的分片中,算法内部是把记录分片到一种叫做"bucket"(hash桶)的内部算法结构中的,然后hash桶与实际的分片节点一一对应,从此实现了分片.路由的功 ...

  10. 笔记:MyBatis Mapper XML文件详解 - Result Maps

    Result Maps(结果集) resultMap 元素是 MyBatis 中最重要最强大的元素.它就是让你远离 90%的需要从结果 集中取出数据的 JDBC 代码的那个东西, 而且在一些情形下允许 ...