[暑假集训--数论]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.
poj 2142
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
相关文章推荐
- [暑假集训--数论]poj3518 Prime Gap
- [暑假集训--数论]poj2909 Goldbach's Conjecture
- [暑假集训--数论]poj2262 Goldbach's Conjecture
- [暑假集训--数论]poj1595 Prime Cuts
- [暑假集训--数论]poj2034 Anti-prime Sequences
- [暑假集训--数论]poj1365 Prime Land
- [暑假集训--数论]poj2115 C Looooops
- [暑假集训--数论]hdu1019 Least Common Multiple
- [暑假集训--数论]hdu2136 Largest prime factor
- 暑假集训-组合数学及数论
- [暑假集训--数论]poj1061 青蛙的约会
- [2016/8/2][暑假集训]数论基础
- [暑假集训--数论]poj2773 Happy 2006
- [暑假集训--数论]poj2657 Comfort
- [暑假集训--数论]poj1730 Perfect Pth Powers
- 暑假集训-组合数学及数论
- 2017CSU暑假集训二 G-River Hopscotch
- Monthly Expense--CSU-ACM2017暑假集训2-二分搜索
- R2D2 and Droid Army--CSU-ACM2017暑假集训比赛1
- ZZULI-暑假集训计划及相关规定[version 1.1]