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. 提高mysql千万级大数据SQL查询优化几条经验

    凯哥java                             微信号                             kaigejava 功能介绍                    ...

  2. linux下用scp命令在两个服务器之间传输文件,利用php_scp函数进行文件传输

    在linux下利用scp进行文件传输, 从服务器下载文件 scp username@servername:/path/filename /path/filename 上传本地文件到服务器 scp /p ...

  3. 【sqli-labs】 less60 GET -Challenge -Double Query -5 queries allowed -Variation3 (GET型 挑战 双查询 只允许5次查询 变化3)

    http://192.168.136.128/sqli-labs-master/Less-60/?id=1")%23 http://192.168.136.128/sqli-labs-mas ...

  4. mysql_数据查询_连接查询

    连接查询 1.连接(join) 也称θ连接,从两个关系的笛卡尔积中选择属性间满足一定条件的元组. 等值连接:θ为“=”的连接运算称为等值连接.从关系R和S的广义笛卡尔积中选取A.B属性值相等的元组. ...

  5. vue项目中使用百度地图的方法

    1.在百度地图申请密钥: http://lbsyun.baidu.com/  将 <script type="text/javascript" src="http: ...

  6. zepto处理touch事件

    处理Touch事件能让你了解到用户的每一根手指的位置,在touch事件触发的时候产生,可以通过touch event handler的event对象取到,如果基于zepto.js开发,一般是通过eve ...

  7. JTable设置表格背景颜色——隔行不同

    package view; import java.awt.Color; import java.awt.Component; import javax.swing.JLabel; import ja ...

  8. 用jquery-easyui的布局layout写后台管理页面

    先在官网下载easyui文档 引入头部文件 <link rel="stylesheet" type="text/css" href="${pag ...

  9. C - CJSON

    cJSON   API 说明 cJSON_Version() 获得cJSON的版本 cJSON_InitHooks(); 初始化cJSON_Hooks结构体 cJSON_Parse(); 将字符串解析 ...

  10. hdu 4876

    ZCC loves cards Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)T ...