博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Poj 2142 The Balance 扩展欧几里得
阅读量:3904 次
发布时间:2019-05-23

本文共 2589 字,大约阅读时间需要 8 分钟。

Description

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 200500 200 300500 200 500275 110 330275 110 385648 375 40023 1 100000 0 0

Sample Output

1 31 11 00 31 149 743333 1

 代码如下:

/*求 a*x+b*y=d;x+y所得的最小整数分别求x和y最小整数解然后分别代入y与x中求解*/#include 
#include
#include
#include
#include
using namespace std;typedef long long ll;int a,b,d;int Extend (int A,int B,int& X,int& Y){ if(B==0) { X=1; Y=0; return A; } else { int temp,ans; ans=Extend(B,A%B,X,Y); temp=X; X=Y; Y=temp-A/B*Y; return ans; }}int main(){ while (scanf("%d%d%d",&a,&b,&d)&&(a||b||d)) { int x,y; int x1,y1,x2,y2; int Gcd=Extend(a,b,x,y); int t=d/Gcd; int aa=a/Gcd,bb=b/Gcd; x=x*t; y=y*t; x1=(x%bb+bb)%bb; y1=abs((d-a*x1)/b); y2=(y%aa+aa)%aa; x2=abs((d-b*y2)/a); if(x1+y1

 

转载地址:http://iaaen.baihongyu.com/

你可能感兴趣的文章
ubuntu 下 codeblocks 的使用 各种技巧转自
查看>>
win7下 背景色更改为护眼颜色
查看>>
最小二乘法拟合圆公式推导及vc实现
查看>>
Google搜索使用技巧
查看>>
【HTML】网页中嵌入视频
查看>>
日行一善的100种方式
查看>>
pdflatex插入EPS格式图片的两种方法
查看>>
在博客中用latex写公式
查看>>
Windows 7 虚拟串口 VSPD 6
查看>>
Matlab图像处理小结
查看>>
【ROS】Learning tf教程各部分结果
查看>>
ROS资源
查看>>
安装ARBOTIX SIMULATOR
查看>>
Python初学者
查看>>
Python初学者(续1)
查看>>
Python初学者(续2)
查看>>
ROS下实现timed_out_and_back功能
查看>>
四元数与旋转
查看>>
ROS 学习系列 -- RViz中移动机器人来学习 URDF,TF,base_link, map,odom和odom 主题的关系
查看>>
OpenCV图像的轮廓的匹配
查看>>