poj 2142 The Balance
Time Limit: 5000MS | Memory Limit: 65536K | |
Description
You are asked to help her by calculating how many weights are required.

Input
The end of the input is indicated by a line containing three zeros separated by a space. It is not a dataset.
Output
- 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
#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的更多相关文章
- POJ.2142 The Balance (拓展欧几里得)
POJ.2142 The Balance (拓展欧几里得) 题意分析 现有2种质量为a克与b克的砝码,求最少 分别用多少个(同时总质量也最小)砝码,使得能称出c克的物品. 设两种砝码分别有x个与y个, ...
- POJ 2142 The Balance(exgcd)
嗯... 题目链接:http://poj.org/problem?id=2142 AC代码: #include<cstdio> #include<iostream> using ...
- POJ 2142 The Balance【扩展欧几里德】
题意:有两种类型的砝码,每种的砝码质量a和b给你,现在要求称出质量为c的物品,要求a的数量x和b的数量y最小,以及x+y的值最小. 用扩展欧几里德求ax+by=c,求出ax+by=1的一组通解,求出当 ...
- POJ 2142 The Balance (解不定方程,找最小值)
这题实际解不定方程:ax+by=c只不过题目要求我们解出的x和y 满足|x|+|y|最小,当|x|+|y|相同时,满足|ax|+|by|最小.首先用扩展欧几里德,很容易得出x和y的解.一开始不妨令a& ...
- POJ 2142 The balance | EXGCD
题目: 求ax+by=c的一组解,使得abs(x)+abs(y)尽量小,满足前面前提下abs(ax)+abs(by)尽量小 题解: exgcd之后,分别求出让x尽量小和y尽量小的解,取min即可 #i ...
- POJ - 2142 The Balance(扩展欧几里得求解不定方程)
d.用2种砝码,质量分别为a和b,称出质量为d的物品.求所用的砝码总数量最小(x+y最小),并且总质量最小(ax+by最小). s.扩展欧几里得求解不定方程. 设ax+by=d. 题意说不定方程一定有 ...
- POJ 2142 - The Balance [ 扩展欧几里得 ]
题意: 给定 a b n找到满足ax+by=n 的x,y 令|x|+|y|最小(等时令a|x|+b|y|最小) 分析: 算法一定是扩展欧几里得. 最小的时候一定是 x 是最小正值 或者 y 是最小正值 ...
- The Balance POJ 2142 扩展欧几里得
Description Ms. Iyo Kiffa-Australis has a balance and only two kinds of weights to measure a dose of ...
- POJ 2142:The Balance
The Balance Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 4781 Accepted: 2092 Descr ...
随机推荐
- 【洛谷1026】【NOIP2001】统计单词个数
题面 题目描述 给出一个长度不超过200的由小写英文字母组成的字母串(约定;该字串以每行20个字母的方式输入,且保证每行一定为20个).要求将此字母串分成k份(1<k<=40),且每份中包 ...
- Zabbix 微信报警Python版(带监控项波动图片)
#!/usr/bin/python # -*- coding: UTF- -*- #Function: 微信报警python版(带波动图) #Environment: python import ur ...
- HTTP架构介绍(1) Web服务器和代理服务器
HTTP应用协议本身是不能运行的,它需是需要架构在硬件和软件解决方案上,才能在万维网上提供高效的传输服务. 在这系列的文章中,我们将会了解到以下概念: Web服务器 代理服务器 缓存 网关.信道和中继 ...
- 深入理解JVM垃圾收集机制(JDK1.8)
垃圾收集算法 标记-清除算法 最基础的收集算法是"标记-清除"(Mark-Sweep)算法,分两个阶段:首先标记出所有需要回收的对象,在标记完成后统一回收所有被标记的对象. 不足: ...
- 【原】无脑操作:IDEA + maven + SpringBoot + JPA + EasyUI实现CRUD及分页
背景:上一篇文章的界面太丑.没有条件查询功能.所以做一些改进,整合EasyUI做实现.(仅以此文纪念表格中出现的这些朋友工作六周年,祭奠一下逝去的青春^_^) 一.开发环境(参照上一篇文章) 补充:E ...
- IIS前端页面不显示详细错误解决方法
要想解决这个问题,有三种方法可以考虑: 1.Internet信息服务(IIS)管理器 2.Web.config文件 3. 命令行 在IIS的"错误页"右边的"编辑功能设置 ...
- [转]使用Subversion进行版本控制
使用Subversion进行版本控制 http://www.subversion.org.cn/svnbook/1.4/index.html 针对 Subversion 1.4(根据r2866编译) ...
- java web需要好好掌握的一些东西
这是一些需要好好的复习的东西 本来存了个文档 怕整丢了 就在这里保存一下 java 基础 重点关注集合 如list hashmap等使用(有时间多看看hashmap的实现原理 问的比较多)多线程 ...
- c# 根据唯一码,存缓存 实现12小时内 阅读量+1
需求:某一个详细页面需要实现用户 12小时内阅读量+1, 实现思路;得到一个唯一码的机器码,不管是否用户登录了 都有这个码,然后存到缓存里面 最后判断时间+12小时 是否超过当前时间 string ...
- 20165226 预备作业3 Linux安装及学习
20165226 预备作业3 Linux安装及学习 Linux安装 一.下载安装VirtualBox 1.首先在官网下载,点开基于VirtualBox虚拟机安装Ubuntu图文教程 找到版本进行下载安 ...