您的位置:首页 > 其它

[暑假集训--数论]poj2142 The Balance

2017-08-04 15:15 281 查看
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.

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: