uva10104 - Euclid Problem

来源:互联网 发布:mac 日历 广告 编辑:程序博客网 时间:2024/05/01 09:51

 Euclid Problem 

The Problem

From Euclid it is known that for any positive integers A and B there exist such integers X and Y thatAX+BY=D, where D is the greatest common divisor of A and B. The problem is to find for given A and Bcorresponding XY and D.

The Input

The input will consist of a set of lines with the integer numbers A and B, separated with space (A,B<1000000001).

The Output

For each input line the output line should consist of three integers X, Y and D, separated with space. If there are several such X and Y, you should output that pair for which |X|+|Y| is the minimal (primarily) andX<=Y (secondarily).

Sample Input

4 617 17

Sample Output

-1 1 20 1 17
#include <iostream>#include <cstdio>#include <cstring>#include <string>#include <algorithm>using namespace std;#define ll long longll A , B , D , X , Y;ll gcd(ll a , ll b){    if(b == 0){        X = 1;        Y = 0;        return a;    }else{        ll t = gcd(b , a%b);        ll tX = Y;        Y = X-(a/b)*Y;        X = tX;        return t;    }}void computing(){    int T = false;    if(A < B){        T = true;        swap(A , B);    }    D = gcd(A , B);    if(T) swap(X , Y);    cout << X << " " << Y << " " << D << endl;}int main(){    while(cin >> A >> B){        computing();    }    return 0;}


0 0
原创粉丝点击