欢迎访问我的新博客: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的更多相关文章

  1. (Step1-500题)UVaOJ+算法竞赛入门经典+挑战编程+USACO

    http://www.cnblogs.com/sxiszero/p/3618737.html 下面给出的题目共计560道,去掉重复的也有近500题,作为ACMer Training Step1,用1年 ...

  2. ACM训练计划step 1 [非原创]

    (Step1-500题)UVaOJ+算法竞赛入门经典+挑战编程+USACO 下面给出的题目共计560道,去掉重复的也有近500题,作为ACMer Training Step1,用1年到1年半年时间完成 ...

  3. 算法竞赛入门经典+挑战编程+USACO

    下面给出的题目共计560道,去掉重复的也有近500题,作为ACMer Training Step1,用1年到1年半年时间完成.打牢基础,厚积薄发. 一.UVaOJ http://uva.onlinej ...

  4. HDU 1525 Euclid's Game 博弈

    Euclid's Game Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tot ...

  5. hdu------(1525)Euclid's Game(博弈决策树)

    Euclid's Game Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tot ...

  6. HDU 1525 Euclid's Game (博弈)

    Euclid's Game Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tot ...

  7. poj 2348 Euclid's Game 题解

    Euclid's Game Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9023   Accepted: 3691 Des ...

  8. BNU 4356 ——A Simple But Difficult Problem——————【快速幂、模运算】

    A Simple But Difficult Problem Time Limit: 5000ms Memory Limit: 65536KB 64-bit integer IO format: %l ...

  9. BNU 28887——A Simple Tree Problem——————【将多子树转化成线段树+区间更新】

    A Simple Tree Problem Time Limit: 3000ms Memory Limit: 65536KB This problem will be judged on ZJU. O ...

随机推荐

  1. 汉高澳大利亚sinox接口捆绑经典winxp,全面支持unicode跨语言处理

    用qtconfig(或者qtconfig-qt4)设置字体后,汉澳sinox视窗界面以跟winxp媲美的界面出现,爽心悦目. 并且视窗使用非常稳定.非常少出现死机无响应现象,堪称完美. 引入unico ...

  2. Xcode-5.1.1更改文件盯作者

    原来的文件默认是用户开机时的username ,网上说什么改通讯录事实上都是不正确的. 1.首先打开偏好设置,选择用户群组 2.进入用户界面 改动全名.此时要求你输入用户的password才干改动us ...

  3. httpclient总结

    1.httpclient总结:一.基本知识准备(1)构建URI工具类,URIBuilder(2)HttpResponse类,可以添加Header信息 获取所有Header信息的方法,调用HeaderI ...

  4. Js 对象添加属性

    var arr = new Array(); arr[0] = jQuery("#data1").val(); var obj = {}; obj.y='abc'; arr.pus ...

  5. 2014在百度之星资格赛的第四个冠军Labyrinth

    Problem Description 熊度仅仅是一种冒险的熊,一个偶然落入一个m*n迷宫矩阵,能从矩阵左上角第一个方格開始走,仅仅有走到右上角的第一个格子才算走出迷宫.每一次仅仅能走一格,且仅仅能向 ...

  6. Android启动第三方应用程序

    主要是开始通过包名的第三方应用程序,获取的方法的包名是非常在线.不是说. 两种方式启动: 第一: Intent intent = new Intent(); intent.setClassName(& ...

  7. Android学习----异常(2):Please ensure that adb is correctly located at &#39; ... &#39;

    打开任务管理器,在后台进程中找到 kadb,结束这个进程,重新启动eclipse.

  8. 通俗易懂的语言描述JavaScript原型

    这是一个翻译.原文地址http://javascriptissexy.com/javascript-prototype-in-plain-detailed-language/# 原型(prototyp ...

  9. Nagios监控lvs服务

    1在lvs server上安装nrpe客户端: 1.1,rpm方式安装nrpe客户端 下载地址:http://download.csdn.net/detail/mchdba/7493875 [root ...

  10. Net Framework中的提供的常用委托类型

    .Net Framework中的提供的常用委托类型   .Net Framework中提供有一些常用的预定义委托:Action.Func.Predicate.用到委托的时候建议尽量使用这些委托类型,而 ...