欢迎访问我的新博客: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. 用jQuery写了一个模态框插件

    用jQuery写了一个模态框插件 大家觉得下面的框框好看么, 水印可以去掉(这个任务交给你们了(- o -)~zZ); "info"框 $("div").con ...

  2. DevExpress Report 其他常用设计技巧

    原文:DevExpress Report 其他常用设计技巧 1 设置默认的打印纸张及页边距 选择Report-打开属性窗口,设置默认边距(Margins)和默认纸张(PaperKind). 2 修改R ...

  3. javascript实现数据结构:广义表

    原文:javascript实现数据结构:广义表  广义表是线性表的推广.广泛用于人工智能的表处理语言Lisp,把广义表作为基本的数据结构. 广义表一般记作: LS = (a1, a2, ..., an ...

  4. TRILL浅析

    1 TRILL概述 TRILL的全称就是Transparent Interconnection of Lots of Links,顾名思义,其本质就是将非常多条链路透明地组织在一起,以致于上层IP应用 ...

  5. poj 2828 Buy Tickets(树状数组 | 线段树)

    题目链接:poj 2828 Buy Tickets 题目大意:给定N,表示有个人,给定每一个人站入的位置,以及这个人的权值,如今按队列的顺序输出每一个人的权值. 解题思路:第K大元素,非常巧妙,将人入 ...

  6. UIApplicationMain方法介绍

    在IOS程序的main函数中执行了一个UIApplicationMain这个函数,下面介绍以下这个函数的作用. int UIApplicationMain(int argc, char *argv[] ...

  7. gnu 扩展之#和##

    转自:http://chongsoft.bokee.com/5816474.html 今天测试了宏定义中的 "#" 和 "##" 的区别. 结果如下: &quo ...

  8. Java对象比较器对泛型List进行排序-Demo

    针对形如:字段1 字段2 字段3 字段n 1 hello 26 7891 world 89 5562 what 55 4562 the 85 452 fuck 55 995 haha 98 455 以 ...

  9. POJ 3070 Fibonacci(矩阵高速功率)

    职务地址:POJ 3070 用这个题学会了用矩阵高速幂来高速求斐波那契数. 依据上个公式可知,第1行第2列和第2行第1列的数都是第n个斐波那契数.所以构造矩阵.求高速幂就可以. 代码例如以下: #in ...

  10. Oracle 11g 客户端的安装和配置。

    原文:Oracle 11g 客户端的安装和配置. 数据库和客户端在不同的机器之上. 在安装之前,在安装Oracle数据库的服务器上导航到下面的目录. 将listener.ora和tnsnames.or ...