POJ 2142
来源:互联网 发布:mysql优化 编辑:程序博客网 时间:2024/06/15 16:37
Description
You are asked to help her by calculating how many weights are required.
Input
The end of the input is indicated by a line containing three zeros separated by a space. It is not a dataset.
Output
- 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
因为要比较大小,所以分别交换系数的值比较两组未知数,因为有常数调整所以整体最小,一定有一个未知数最小
#include <iostream>#include <cstdio>#include <cmath>#include <algorithm>#include <cstring>using namespace std;int ext_gcd(int a,int b,int &x,int &y);int cal(int a,int b,int c);int main(){ int a, b, d; while(scanf("%d %d %d", &a, &b, &d),a||b||d) { int x, y, sum1, ans1, x1, y1, sum2, ans2; x=cal(a,b,d); y=abs((d-a*x)/b); sum1=x+y; ans1=a*x+b*y; x1=cal(b,a,d); y1=abs((d-b*x1)/a); sum2=x1+y1; ans2=b*x1+a*y1; if(sum1<sum2) { printf("%d %d\n",x,y); } else if(sum1==sum2&&ans1<ans2) { printf("%d %d\n",x,y); } else { printf("%d %d\n",y1,x1); } } return 0;}int cal(int a,int b,int c){ int x, y; int d=ext_gcd(a,b,x,y); b/=d; x*=(c/d); if(b<0) { b=-b; } x%=b; if(x<0) { x+=b; } return x;}int ext_gcd(int a,int b,int &x,int &y){ if(b==0) { x=1; y=0; return a; } int ans=ext_gcd(b,a%b,x,y); int tmp=x; x=y; y=tmp-a/b*y; return ans;}
- poj 2142
- POJ 2142
- poj-2142-
- poj 2142
- poj 1061 + poj 2115 + poj 2142 (exgcd)
- POJ 2142 扩展欧几里得
- poj - 2142 - The Balance
- POJ 2142 The Balance
- POJ 2142 The Balance
- POJ 2142 The Balance
- poj 2142 The Balance
- The Balance POJ 2142
- POJ 2142 The Balance
- poj 2142 The Balance
- POJ 2142 扩展欧几里得
- POJ 2142:The Balance
- poj 2142 The Balance
- POJ 2142 The Balance
- Activiti(七)——排他网关(ExclusiveGateWay)
- 【杭电 5805 8.6 BC B NanoApe Loves Sequence】
- 基于tcl tk 的HyperGraph二次开发
- jquery animate step 实现 transform css3方法
- Android Studio设置自动编译工程
- POJ 2142
- codeforces 17D. Notepad 欧拉函数降幂
- Dom4j解析XML
- 深入理解java异常处理机制
- 类和类之间的关系
- android---------菜鸟集成环信(头像,昵称)
- hdu 5800 To My Girlfriend (dp)
- 数学在计算机图形学中的应用
- Ubuntu安装xgboost