本文共 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.
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/