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
题意:有题意列方程为
a*x1+d=b*y1;
b*x2+d=a*y2;
求minn(x1+y1,x2+y2);
分别用扩展欧几里得求出x1,y1,x2,y2;然后比较两大小,输出较小的一组;
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<queue>
#include<map>
#include<set>
#include<vector>
#include<cstdlib>
#include<string>
#define eps 0.000000001
typedef long long ll;
typedef unsigned long long LL;
using namespace std;
ll abs1(ll n){
if(n<)n=-n;
return n;
}
ll gcd(ll a,ll b){
if(b==)return a;
else{
return gcd(b,a%b);
}
}
ll exgcd(ll a,ll b,ll &x,ll &y){
if(b==){
x=;y=;return a;
}
ll r=exgcd(b,a%b,x,y);
int t=y;
y=x-(a/b)*y;
x=t;
return r;
}
int main(){
ll a,b,c;ll x1,y1,x2,y2;
while(scanf("%I64d%I64d%I64d",&a,&b,&c)!=EOF){
if(a==&&b==&&c==)break;
ll r1=exgcd(a,b,x1,y1);
ll r2=exgcd(b,a,x2,y2);
x1=x1*c/r1;
x2=x2*c/r2;
ll t1=b/r1;
x1=(x1%t1+t1)%t1;
y1=abs1((a*x1-c)/b);
ll t2=a/r2;
x2=(x2%t2+t2)%t2;
y2=abs1((b*x2-c)/a);
if(x1+y1<x2+y2)cout<<x1<<" "<<y1<<endl;
else{
cout<<y2<<" "<<x2<<endl;
}
}
}

poj 2142的更多相关文章

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

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

  2. poj 2142 The Balance

    The Balance http://poj.org/problem?id=2142 Time Limit: 5000MS   Memory Limit: 65536K       Descripti ...

  3. POJ 2142 The Balance(exgcd)

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

  4. poj 2142 拓展欧几里得

    #include <cstdio> #include <algorithm> #include <cstring> #include <iostream> ...

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

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

  6. poj 2142 扩展欧几里得解ax+by=c

    原题实际上就是求方程a*x+b*y=d的一个特解,要求这个特解满足|x|+|y|最小 套模式+一点YY就行了 总结一下这类问题的解法: 对于方程ax+by=c 设tm=gcd(a,b) 先用扩展欧几里 ...

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

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

  8. POJ 2142:The Balance_扩展欧几里得(多组解)

    先做出两个函数的图像,然后求|x|+|y|的最小值.|x|+|y|=|x0+b/d *t |+|y0-a/d *t| 这个关于t的函数的最小值应该在t零点附近(在斜率大的那条折线的零点附近,可以观察出 ...

  9. E - The Balance POJ - 2142 (欧几里德)

    题意:有两种砝码m1, m2和一个物体G,m1的个数x1,  m2的个数为x2, 问令x1+x2最小,并且将天平保持平衡 !输出  x1 和 x2 题解:这是欧几里德拓展的一个应用,欧几里德求不定方程 ...

  10. 扩展欧几里得(E - The Balance POJ - 2142 )

    题目链接:https://cn.vjudge.net/contest/276376#problem/E 题目大意:给你n,m,k,n,m代表当前由于无限个质量为n,m的砝码.然后当前有一个秤,你可以通 ...

随机推荐

  1. 2016.01.05 DOM笔记(一) 查找元素

    DOM节点的种类 元素和标签是一个意思,例如<body>标签或者称为<body>元素 节点DOM的节点分为三类  元素节点,文本节点,属性节点 例如 <div id=‘b ...

  2. Lazarus Coolbar and AnchroDocking

    在lazarus1.6里加载了AnchroDocking后,Coolbar突然不见了,找了好久没找到,原来在这里! 在AnchroDocking中可能是为了界面的最大化,默认是开始Toolbar 而关 ...

  3. Django 更新字段

    Django在1.7以后的版本提供数据迁移命令,用来在修改模型中的字段,更新到数据库 1. python manager.py makemigrations 命令用来创建迁移文件版本的 2. pyth ...

  4. JS 获得节点

    var ele = ev.parentNode; var elem_child = ele.childNodes; in elem_child) { //遍历子元素数组 if (elem_child[ ...

  5. jboss-eap-6.2修改端口号

    最近要改版一个项目,用来配合日常工作使用,需要在服务器上放多个jboss,那么就需要修改jboss的端口,如果服务器上配置了JBOSS_HOME,需要先删除,否则配置修改不会生效,会依然用老的jbos ...

  6. CPU 指令集(Instruction Set Architecture, ISA)

    本文摘自网络 概念 指令集是存储在CPU内部,对CPU运算进行指导和优化的硬程序,用来引导CPU进行加减运算和控制计算机操作系统的一系列指令集合.拥有这些指令集,CPU就可以更高效地运行.系统所下达的 ...

  7. JDK5-8特性归纳

    jdk5新特性1.自动装箱和拆箱2.枚举3.静态导入4.可变参数5.內省   是Java语言对Bean类属性.事件的一种缺省处理方法.例如类A中有属性那么,那我们可以通过getName,setName ...

  8. C#关键字详解第三节

    byte:字节 字节是计算机信息技术用于计量存储容量的一种计量单位,通常情况下一字节等于八位,也在一些计算机编程 语言中表示数据类型和语言字符.这是百度百科给出的解释,在C#语言中byte也可以是一种 ...

  9. 【郑轻邀请赛 G】密室逃脱

    [题目链接]:https://acm.zzuli.edu.cn/zzuliacm/problem.php?id=2133 [题意] [题解] 考虑每一个二进制数的最高位->第i位; 肯定是1(这 ...

  10. [JLOI2015]战争调度

    [JLOI2015]战争调度 题目 解题报告 考试打了个枚举的暴力,骗了20= = $qsy$大佬的$DP$: 其实就是枚举= =,只不过枚举的比较强= = #include<iostream& ...