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. 【BZOJ3930】选数(莫比乌斯反演,杜教筛)

    [BZOJ3930]选数(莫比乌斯反演,杜教筛) 题面 给定\(n,K,L,R\) 问从\(L-R\)中选出\(n\)个数,使得他们\(gcd=K\)的方案数 题解 这样想,既然\(gcd=K\),首 ...

  2. Bzoj2134:单选错位

    题面 Bzoj Sol 第\(i\)道题选对的概率就是\(\frac{min(a[i-1], a[i])}{a[i]*a[i-1]}\) # include <bits/stdc++.h> ...

  3. C++ RCSP智能指针简单实现与应用

    智能指针的实现代码来源博客:<http://blog.csdn.net/to_be_better/article/details/53570910> 修改:添加 get()函数,用以获得原 ...

  4. mysql性能分析之explain的用法

    之前是一直没有听过explain这个关键字的, 最近因为项目中总是会有慢查询的一些操作, 所以请教了旁边的同事帮忙排查下原因, 看到同事用explain来分析一些sql语句, 感觉好像发现了新大陆一样 ...

  5. Java集合框架(二)

    原文  http://www.jianshu.com/p/2070cb32accb List接口 查阅API,看 List 的介绍.有序的 collection (也称为序列).此接口的用户可以对列表 ...

  6. win10环境下利用pyinstaller把python代码(.py)打包成可执行文件(.exe)

    前言 最近写了一个小小的检测程序,python写起来只需要短短一百行,可是打包起来就没有C那么容易了.下面记录一下我艰难的"打包"过程. 方法一:py2exe py2exe是一种经 ...

  7. Java反射总结

    一. 获取Class对象的3种方法: 1. Class.forName("");例如:Class.forName("java.lang.String"); 2. ...

  8. delphi JPG图片 旋转 切边 缩放

    unit UCutFigure_JPG; //JPG 切图 interface uses Windows, Messages, SysUtils, Variants, Classes, Graphic ...

  9. 使用openssl演练数字签名

    以下代码摘自网上,设置一个server和client,client代码如下: package main import (    "fmt"    "io/ioutil&q ...

  10. Axis1.4之定制发布服务

    将axis1.4_home\webapps目录下的axis文件夹拷贝到tomcat_home\webapps目录下.然后在tomcat_home\webapps\axis\WEB-INF\lib下添加 ...