Euclid Problem - PC110703
欢迎访问我的新博客:http://www.milkcu.com/blog/
原文地址:http://www.milkcu.com/blog/archives/uva10104.html
原创:Euclid Problem - PC110703
作者:MilkCu
题目描述
| Euclid Problem |
The Problem
From Euclid it is known that for any positive integers A and Bthere exist such integersX andY that
AX+BY=D,where D is the greatest common divisor ofA andB.The problem is to find for given
A and B correspondingX,Y and D.
The Input
The input will consist of a set of lines with the integer numbers A andB, separated with space (A,B<1000000001).
The Output
For each input line the output line should consist of threeintegers X, Y andD, separated with space. If there are severalsuchX and
Y, you should output that pair for which|X|+|Y| is the minimal (primarily) andX<=Y (secondarily).
Sample Input
4 6
17 17
Sample Output
-1 1 2
0 1 17
解题思路
本题考查的是求解最大公约数(greatest common divisor,也称gcd)的欧几里得(Euclid)算法。
Euclid算法建立在两个事实的基础上:
1. 如果b|a(b整除a),则gcd(a, b) = b;
2. 如果存在整数t和r,使得a = bt + r,则gcd(a, b) = gcd(b, r)。
Euclid算法是递归的,它不停的把较大的整数替换为它除以较小整数的余数。
Euclid算法指出,存在x和y,使
a * x + b * y = gcd(a, b)
又有
gcd(a, b) = gcd(b, a % b)
为了方便计算,我们将上式写为
gcd(a, b) = gcd(b, a - b * floor(a / b))
假设我们已经找到整数x'和y',使得
b * x' + (a - b * floor(a / b)) * y' = gcd(a, b)
整理上式,得到
a * y' + b * (x' - b * floor(a / b) * y') = gcd(a, b)
与a * x + b * y = gcd(a, b)相对应,得到
x = y'
y = x' - floor(a / b) * y'
代码实现
#include <iostream>
#include <cmath>
#include <algorithm>
using namespace std;
int gcd(int a, int b, int & x, int & y) {
int x1, y1;
if(a < b) {
return gcd(b, a, y, x);
}
if(b == 0) {
x = 1;
y = 0;
return a;
}
int g = gcd(b, a % b, x1, y1);
x = y1;
y = x1 - floor(a / b) * y1;
return g;
}
int main(void) {
int a, b;
while(cin >> a >> b) {
int x, y;
int g = gcd(a, b, x, y);
cout << x << " " << y << " " << g << endl;
}
return 0;
}
(全文完)
本文地址:http://blog.csdn.net/milkcu/article/details/23590217
Euclid Problem - PC110703的更多相关文章
- (Step1-500题)UVaOJ+算法竞赛入门经典+挑战编程+USACO
http://www.cnblogs.com/sxiszero/p/3618737.html 下面给出的题目共计560道,去掉重复的也有近500题,作为ACMer Training Step1,用1年 ...
- ACM训练计划step 1 [非原创]
(Step1-500题)UVaOJ+算法竞赛入门经典+挑战编程+USACO 下面给出的题目共计560道,去掉重复的也有近500题,作为ACMer Training Step1,用1年到1年半年时间完成 ...
- 算法竞赛入门经典+挑战编程+USACO
下面给出的题目共计560道,去掉重复的也有近500题,作为ACMer Training Step1,用1年到1年半年时间完成.打牢基础,厚积薄发. 一.UVaOJ http://uva.onlinej ...
- HDU 1525 Euclid's Game 博弈
Euclid's Game Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tot ...
- hdu------(1525)Euclid's Game(博弈决策树)
Euclid's Game Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tot ...
- HDU 1525 Euclid's Game (博弈)
Euclid's Game Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tot ...
- poj 2348 Euclid's Game 题解
Euclid's Game Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9023 Accepted: 3691 Des ...
- BNU 4356 ——A Simple But Difficult Problem——————【快速幂、模运算】
A Simple But Difficult Problem Time Limit: 5000ms Memory Limit: 65536KB 64-bit integer IO format: %l ...
- BNU 28887——A Simple Tree Problem——————【将多子树转化成线段树+区间更新】
A Simple Tree Problem Time Limit: 3000ms Memory Limit: 65536KB This problem will be judged on ZJU. O ...
随机推荐
- windows server 2008见安装IIS方法(解决)
windows server 2008见安装IIS方法(解决) 刚开始有点蒙,后来才知道原来如此.! . 右键点击[我的电脑]--[管理]--[字符]--[加入角色]--仅落后win7像.啊! 版权声 ...
- Android 滑动界面实现---Scroller类别 从源代码和开发文档了解(让你的移动布局)
在android学习,行动互动是软件的重要组成部分,其中Scroller是提供了拖动效果的类,在网上.比方说一些Launcher实现滑屏都能够通过这个类去实现.. 样例相关博文:Android 仿 窗 ...
- vi/vim编辑器的基本操作
vi/vim编辑器的基本操作 Contents 1. 工具准备(下载gvim) 2. vi/vim基本入门 2.1. 安装 2.2. 基本使用 3. vi/vim基本命令表 1 工具准备(下载gvim ...
- Spring环境配置
研究spring3的时候发现一个非常好用的特性:环境配置(spring2是否有此特性未知) 官方演示样例代码例如以下: <!-- app-config.xml --> <beans ...
- C# 一个WCF简单实例
以订票为例简单应用wcf 新建一个wcf服务应用程序 在IService1.cs定义服务契约 复制代码 代码如下: namespace WcfDemo { // 注意: 如果更改此处的接口名称 &qu ...
- 绑定枚举到dropdownlist
pageTools.BindEnumToDropdownList(typeof(enumDealerArea), ddlBmwArea, new ListItem("--请选择--" ...
- Linux内核和根文件系统引导加载程序
续博文<u-boot之u-boot-2009.11启动过程分析> Linux内核启动及文件系统载入过程 当u-boot開始运行bootcmd命令.就进入Linux内核启动阶段,与u-boo ...
- hdu1881 毕业bg(深搜索dfs)
主题链接:pid=1881">http://acm.hdu.edu.cn/showproblem.php? pid=1881 ----------------------------- ...
- 【足迹C++primer】49、超载,变化,运营商
超载,变化,运营商 Conversion Operators 转换操作符 operator type() const Conversions to an array or a function typ ...
- 通俗易懂的语言描述JavaScript原型
这是一个翻译.原文地址http://javascriptissexy.com/javascript-prototype-in-plain-detailed-language/# 原型(prototyp ...