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和b,现在要称量质量为c,求用最少的砝码或者最轻的砝码称出来,输出a和b的数量X和Y

思路:|X|+|Y|=|X0+t*b/gcd|+|Y0-t*a/gcd|,在|X|取最小或者|Y|取最小时满足题意。

#include<cmath>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<algorithm>
using namespace std;
void ex_gcd(int a,int b,int &d,int &x,int &y)
{
if(b==){x=; y=; d=a; return ;}
ex_gcd(b,a%b,d,y,x); y-=a/b*x;
}
int main()
{
int a,b,c,x,y,gcd;
while(~scanf("%d%d%d",&a,&b,&c)){
if(a==&&b==&&c==) return ;
ex_gcd(a,b,gcd,x,y);
if(c%gcd!=) printf("no solution\n");
else{
int x1=((c/gcd*x)%(b/gcd)+(b/gcd))%(b/gcd);
int y1=(c-a*x1)/b;
int y2=((c/gcd*y)%(a/gcd)+(a/gcd))%(a/gcd);
int x2=(c-b*y2)/a;
x1=abs(x1); y1=abs(y1); x2=abs(x2); y2=abs(y2);
if(x1+y1<x2+y2||x1*a+y1*b<x2*a+y2*b) printf("%d %d\n",x1,y1);
else printf("%d %d\n",x2,y2);
}
}
return ;
}

POJ2142:The Balance (欧几里得+不等式)的更多相关文章

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

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

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

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

  3. SGU 141.Jumping Joe 数论,拓展欧几里得,二元不等式 难度:3

    141. Jumping Joe time limit per test: 0.25 sec. memory limit per test: 4096 KB Joe is a frog who lik ...

  4. POJ - 2142 The Balance(扩展欧几里得求解不定方程)

    d.用2种砝码,质量分别为a和b,称出质量为d的物品.求所用的砝码总数量最小(x+y最小),并且总质量最小(ax+by最小). s.扩展欧几里得求解不定方程. 设ax+by=d. 题意说不定方程一定有 ...

  5. POJ 2142 - The Balance [ 扩展欧几里得 ]

    题意: 给定 a b n找到满足ax+by=n 的x,y 令|x|+|y|最小(等时令a|x|+b|y|最小) 分析: 算法一定是扩展欧几里得. 最小的时候一定是 x 是最小正值 或者 y 是最小正值 ...

  6. SGU 106 The equation 扩展欧几里得好题

    扩展欧几里得的应用……见算法竞赛入门经典p.179 注意两点:1.解不等式的时候除负数变号 2.各种特殊情况的判断( a=0 && b=0 && c=0 ) ( a=0 ...

  7. NOIP2012拓展欧几里得

    拉板题,,,不说话 我之前是不是说过数据结构很烦,,,我想收回,,,今天开始的数论还要恶心,一早上听得头都晕了 先来一发欧几里得拓展裸 #include <cstdio> void gcd ...

  8. Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined) C.Ray Tracing (模拟或扩展欧几里得)

    http://codeforces.com/contest/724/problem/C 题目大意: 在一个n*m的盒子里,从(0,0)射出一条每秒位移为(1,1)的射线,遵从反射定律,给出k个点,求射 ...

  9. poj 1061 青蛙的约会 拓展欧几里得模板

    // poj 1061 青蛙的约会 拓展欧几里得模板 // 注意进行exgcd时,保证a,b是正数,最后的答案如果是负数,要加上一个膜 #include <cstdio> #include ...

随机推荐

  1. codevs——1958 刺激

    1958 刺激  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold 题解       题目描述 Description saffah的一个朋友S酷爱滑雪,并且追求刺 ...

  2. Effective Java P2 Creating and Destroying Objects

    This chapter concerns creating and destorying objects : when and how to create them, when and how to ...

  3. android Activity生命周期的例子

    package com.example.yanlei.yl2; import android.app.AlertDialog; import android.content.DialogInterfa ...

  4. MySQL的字符编码体系(一)——数据存储编码

    安装MySQL好多次了,每次都会纠结于数据库的字符编码配置,所以我决定这一次彻底把它理清. MySQL的字符编码结构比較细,它慷慨向分为两个部分:数据存储编码和传输数据编码.本篇讨论数据存储编码部分, ...

  5. C#如何设置控件水平对齐,垂直对齐

    如果要设置一些控件垂直对齐,点击这个按钮 如果要设置水平对齐,则点击这个按钮,选中控件之后点击左对齐(多个按钮都试下吧,总归能对齐到你要的效果的)

  6. Solidworks如何绘制装饰螺纹线

    1 插入-注解,装饰螺纹线   2 绘制装饰螺纹线,选择螺纹的边线,标准选择ISO,下面可以选择的范围就确定了(M6的孔,只能选择M8的螺纹或者M10的螺纹),画好之后在3D图中并没有明确的螺纹样式 ...

  7. react 创建组件 (三)PureComponet

    我们知道,当组件的props或者state发生变化的时候:React会对组件当前的Props和State分别与nextProps和nextState进行比较,当发现变化时,就会对当前组件以及子组件进行 ...

  8. hdoj-3371-Connect the Cities【最小生成树】

    Connect the Cities Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...

  9. 日常方便使用的Python脚本实现

    目录 文件批量重命名 bin文件合并 正文 1.python根据不同条件批量实现文件重命名 因为下载的电视剧名字比较乱,但却按照下载时间顺序依次排列,而手动重命名或者找软件太麻烦,我就自己实现了个: ...

  10. bzoj1601【Usaco2008 Oct】灌水

    1601: [Usaco2008 Oct]灌水 Time Limit: 5 Sec  Memory Limit: 162 MB Submit: 1589  Solved: 1035 [Submit][ ...