POJ 2142 The Balance .
来源:互联网 发布:yoga lenovo 知乎 编辑:程序博客网 时间:2024/06/05 05:28
题目地址:http://poj.org/problem?id=2142
有两种可能的情况:
a与d放在一起,即 ax+d=by
b与d放在一起,即 ax=by+d
因为x,y是任取的 ,所以解下面一个方程
ax+by=d
利用扩展欧几里得定理,可得两组解,
1. x的最小非负解,及其对应的y
2. y的最小非负解,及其对应的x
因为求的是|x|+|y|的最小值,比较一下就好了
#include<iostream>#include<cmath>#include<cstdio>using namespace std;int gcdEx(int a,int b,int& x,int& y){ //求ax+by=gcd(a,b) 的整数解 返回gcd(a,b); if(b==0) {x=1; y=0; return a;}int x1,y1;int gcd=gcdEx(b,a%b,x1,y1);x=y1;y=x1-a/b*y1;return gcd;}int main(){int a,b,d; int x,y;while(cin>>a>>b>>d){if(a==0&&b==0&&d==0) break;int gd=gcdEx(a,b,x,y);int k=d/gd,s1=b/gd,s2=a/gd;int x1=(k*x%s1+s1)%s1,y1=fabs(d-a*x1)/b; int y2=(k*y%s2+s2)%s2,x2=fabs(d-b*y2)/a; if(x1+y1<x2+y2)cout<<x1<<' '<<y1<<endl;else cout<<x2<<' '<<y2<<endl;}return 0;}
0 0
- 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:The Balance
- poj 2142 The Balance
- POJ 2142 The Balance
- poj 2142 The Balance
- POJ 2142 The Balance
- POJ 2142 The Balance
- POJ 2142 The Balance .
- POJ 2142The Balance
- POJ 2142 The Balance
- POJ 2142 The Balance 拓欧
- HDU 5822 color[Waiting]
- yii2项目实战-博客管理平台的搭建
- 给定一整数在有序的整数数组中找出与给定值最接近的k个整数
- java面试准备-day03-并发包类
- 安卓的通信机制
- POJ 2142 The Balance .
- RDD与DataFrame
- 转:虚幻4引擎3d化UI教程
- 排序检索-10474 - Where is the Marble?
- 让Dev C++支持C++11
- 2092 整数解
- Swift-Swift初体验
- 小娜老师的讲义-Docker基础知识-容器(一)
- windows7 64bite环境安装theano