zhber
有好多做过的题没写下来,如果我还能记得就补吧

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,求ax+by==c,并且输出|a|+|b|最小的方案,如果|a|+|b|相同者输出|ax|+|by|最小的方案

先解出个可行解,然后调整x至x是最小正数,y跟着动,用这个更新下答案。

然后调x到x是最小负数,更新答案

还有y是最小正数、最小负数的情况,更新答案

 1 Select Code
 2 #include<cstdio>
 3 #include<iostream>
 4 #include<cstring>
 5 #include<cstdlib>
 6 #include<algorithm>
 7 #include<cmath>
 8 #include<queue>
 9 #include<deque>
10 #include<set>
11 #include<map>
12 #include<ctime>
13 #define LL long long
14 #define inf 0x7ffffff
15 #define pa pair<int,int>
16 #define mkp(a,b) make_pair(a,b)
17 #define pi 3.1415926535897932384626433832795028841971
18 #define int long long
19 using namespace std;
20 inline LL read()
21 {
22     LL x=0,f=1;char ch=getchar();
23     while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
24     while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
25     return x*f;
26 }
27 LL a,b,c;
28 inline int exgcd(int a,int b,int &x,int &y)
29 {
30     if (!b){x=1;y=0;return a;}
31     int gcd=exgcd(b,a%b,x,y);
32     int t=x;x=y;y=t-a/b*y;
33     return gcd;
34 }
35 inline LL LLabs(LL x){return x<0?-x:x;}
36 inline void work()
37 {
38     LL x,y,ans1=10000000,ans2=10000000;
39     int tt=exgcd(a,b,x,y);
40     if (c%tt!=0)return;
41     x=x*c/tt;y=y*c/tt;
42     int aa=a/tt,bb=b/tt;
43     int d=(x-(x%bb+bb)%bb)/bb;
44     x-=d*bb;y+=d*aa;
45     if (LLabs(x)+LLabs(y)<ans1+ans2||(LLabs(x)+LLabs(y)==ans1+ans2&&LLabs(x)*a+LLabs(y)*b<=ans1*a+ans2*b))ans1=LLabs(x),ans2=LLabs(y);
46     x-=bb;y+=aa;
47     if (LLabs(x)+LLabs(y)<ans1+ans2||(LLabs(x)+LLabs(y)==ans1+ans2&&LLabs(x)*a+LLabs(y)*b<=ans1*a+ans2*b))ans1=LLabs(x),ans2=LLabs(y);
48     d=(y-(y%aa+aa)%aa)/aa;
49     x+=d*bb;y-=d*aa;
50     if (LLabs(x)+LLabs(y)<ans1+ans2||(LLabs(x)+LLabs(y)==ans1+ans2&&LLabs(x)*a+LLabs(y)*b<=ans1*a+ans2*b))ans1=LLabs(x),ans2=LLabs(y);
51     x+=bb;y-=aa;
52     if (LLabs(x)+LLabs(y)<ans1+ans2||(LLabs(x)+LLabs(y)==ans1+ans2&&LLabs(x)*a+LLabs(y)*b<=ans1*a+ans2*b))ans1=LLabs(x),ans2=LLabs(y);
53     printf("%I64d %I64d\n",ans1,ans2);
54 }
55 main()
56 {
57     while (~scanf("%I64d%I64d%I64d",&a,&b,&c)&&a+b+c)work();
58 }
poj 2142

 

——by zhber,转载请注明来源
posted on 2017-08-04 15:15  zhber  阅读(122)  评论(0编辑  收藏  举报