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. Luogu 3375 【模板】KMP字符串匹配(KMP算法)

    Luogu 3375 [模板]KMP字符串匹配(KMP算法) Description 如题,给出两个字符串s1和s2,其中s2为s1的子串,求出s2在s1中所有出现的位置. 为了减少骗分的情况,接下来 ...

  2. [BZOJ2654] tree (kruskal & 二分答案)

    Description 给你一个无向带权连通图,每条边是黑色或白色.让你求一棵最小权的恰好有need条白色边的生成树. 题目保证有解. Input 第一行V,E,need分别表示点数,边数和需要的白色 ...

  3. Idea工具开发 SpringBoot整合JSP(毕设亲测可用)

    因为,临近毕业了,自己虽然也学了很多框架.但是,都是在别人搭建好的基础上进行项目开发.但是springboot的官方文档上明确指出不提倡使用jsp进行前端开发,但是在校期间只学了jsp作为前端页面.所 ...

  4. 某厂java算法题实现及改进【有n个人成一圈,顺序排号(编号为1到n),从第一个人开始报数1到3报数】

    一.第一种实现: 实现比较简单,直接贴现成的代码了,第一种实现: /** * 总人数 * * @param d */ private static void sortQuerry1(int d) { ...

  5. Java注解学习

    一.注解定义 JVM5.0定义了4个标准的元注解: @Target, @Retention, @Documented @Inherited 1. @Target 作用:用于描述注解的使用范围 取值El ...

  6. linux升级python3.6相关命令

    sudo apt-get install python3.6 sudo update-alternatives --install /usr/bin/python python /usr/bin/py ...

  7. centos7安装eclipse方法

    很多喜欢编程的朋友并不是很喜欢使用Windows来编写程序,尽管可视化编程但是操作相对繁琐,因而只在电脑上装有Linux系统,那么我们来说一下Linux下安装Java EE编程工具eclipse的方法 ...

  8. java如何输入数据

    Java程序开发过程中,需要从键盘获取输入值是常有的事,但Java它偏偏就没有像c语言给我们提供的scanf(),C++给我们提供的cin()获取键盘输入值的现成函数!Java没有提供这样的函数也不代 ...

  9. Hive数据仓库笔记(三)

    Joins: Inner  joins: hive> SELECT * FROM sales; Joe 2 Hank 4 Ali 0 Eve 3 Hank 2 hive> SELECT * ...

  10. 属性动画 ValueAnimator 运行原理全解析

    最近下班时间都用来健身还有看书了,博客被晾了一段时间了,原谅我~~~~ 提问环节 好,废话不多说,之前我们已经分析过 View 动画 Animation 运行原理解析,那么这次就来学习下属性动画的运行 ...