POJ 3142 The Balance
Description
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
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
- 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
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <queue>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <set>
using namespace std;
typedef long long LL ;
LL exgcd(LL a,LL b,LL &x,LL &y) ///返回最大公约数
{
if(b==0)
{
x=1;
y=0;
return a;
}
LL r=exgcd(b,a%b,x,y);
// cout<<"x="<<x<<" y="<<y<<endl;
LL t=x;
x=y;
y=t-a/b*y;
return r;
}
int main (){ LL a,b,c;
LL x,y;
while(~scanf("%I64d%I64d%I64d",&a,&b,&c)&&!(a==0&&b==0&&c==0)){
LL mark=0;
if(a<b){
swap(a,b);
mark=1;
}
LL gcd = exgcd(a,b,x,y);
if(c%gcd==0){
y=y*(c/gcd) ;
LL r = a/gcd;
y=(y%r+r)%r;
//cout<<"y="<<y<<" ";
LL y1= y ,x1= (c-b*y1)/a ;
// LL y2= y-r ,x2= (c-b*y2)/a;
LL x2= y-r ,y2= (c-a*x2)/b;
if(x1<0) x1=-x1;
if(y1<0) y1=-y1;
if(x2<0) x2=-x2;
if(y2<0) y2=-y2;
// cout<<"x1 y1 x2 y2 "<<x1<<" "<<y1<<" "<<x2<<" "<<y2<<" "<<endl;
if(x1+y1<x2+y2)
x=x1,y=y1;
else
x=x2,y=y2;
if(mark)
swap(x,y);
printf("%I64d %I64d\n",x,y );
}
}
return 0;
}
POJ 3142 The Balance的更多相关文章
- POJ.2142 The Balance (拓展欧几里得)
POJ.2142 The Balance (拓展欧几里得) 题意分析 现有2种质量为a克与b克的砝码,求最少 分别用多少个(同时总质量也最小)砝码,使得能称出c克的物品. 设两种砝码分别有x个与y个, ...
- poj 2142 The Balance
The Balance http://poj.org/problem?id=2142 Time Limit: 5000MS Memory Limit: 65536K Descripti ...
- POJ 2142 The Balance(exgcd)
嗯... 题目链接:http://poj.org/problem?id=2142 AC代码: #include<cstdio> #include<iostream> using ...
- POJ 1837:Balance 天平DP。。。
Balance Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 11878 Accepted: 7417 Descript ...
- 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 2412 The Balance 【exgcd】By cellur925
题目传送门 一遇到数学就卡住,我这是怎么肥4...(或许到图论会愉悦吧,逃) Description * 给出两种重量为的 A, B 的砝码,给出一种使用最少的砝码的方式,称出重量 C. 我们可以比较 ...
随机推荐
- 分享记录我的Linux系统入门学习经验
人生很多事情都是巧合,或者说命中注定.就拿Linux于我来说,大学期间也修了Linux基础这门课,但是从来没有想到自己会从事与Linux相关的工作,更没有想过自己可以通过Linux获取更多的东西. 真 ...
- canvas 绘制 矩形 圆形
<!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head> <tit ...
- POJ 3694 tarjan 桥+lca
Network Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 7298 Accepted: 2651 Descripti ...
- 黑马程序员——OC语言 内存管理
Java培训.Android培训.iOS培训..Net培训.期待与您交流! (以下内容是对黑马苹果入学视频的个人知识点总结) (一)计数器 每个对象内部都保存了一个与之相关联的整数,称为引用计数器,当 ...
- HttpClientHandler
string url = "http://"; //创建HttpClient(注意传入HttpClientHandler) var handler = new HttpClient ...
- 甘特图和PERT图
gantt图又叫甘特图.进度是按时间顺序计划活动的一个列表,我们称之为Gantt图,它有以下几个关键的成分:1.横跨图顶部排列的是日历表.2.最左边的一列包含了每项任务的标识号(ID).3.左边第二列 ...
- Devexpress datagrid动态添加显示指定列的gridView
代码如下: public class DXGridControlHelper { /// <summary> /// 获取显示指定列的GridView /// </summary&g ...
- Caffe 源碼閱讀(一) Blob.hpp
Blob 四維度(N K H W) N : SGD 一次 mini-batch 個數 K : 如果是圖片表示圖片通道數 如果是中間結果 則理解爲 feature map 個數 H.W : 如果是圖片理 ...
- C++学习笔记21:文件系统
文件系统 实际文件系统 ext, ext2, ext3, ext4 虚拟文件系统 VFS 特殊文件系统/proc:从proc文件系统中抽取信息 实际文件系统:组成与功能描述 引导块,超级块,索引结点区 ...
- meta-analysis 到底是什么个意思类?
背景 科学研究应建立于许多实验结果的重复之上,除了少数新发现外,单个实验结果很难对科学的发展作出极为显著的贡献.所以为了阐明某一主题,在许多科学领域有众多研究者在对不同的实验对象或对同一对象在不同的实 ...