UVa-10104-Euclid Problem

<span style="font-family: Simsun; font-size: 12px; background-color: rgb(255, 255, 255);">The Problem</span>

From Euclid it is known that for any positive integers A and B there exist such integers X and Y that AX+BY=D, where D is the greatest common divisor of A and B. The problem is to find for given A and B corresponding 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) and X<=Y (secondarily).

Sample Input

4 6
17 17

Sample Output

-1 1 2
0 1 17
 
扩展欧几里得模板题
 
当b=0时 返回a 为最大公约数 以及 x=1 y=0 的解使得 a = x*a + b*y  否则扩展gcd 首先算出 d = b*x'+ (a mod b)*y'=a*x+b*y    d = b*x'+ (a-b*a/b)y' = a*y' + b*(x'-a/b*y')
y’ = x
x' - a/b*y' = y
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <string>
#include <algorithm>
#include <queue>
using namespace std;
int a,b;
int d,x,y;
void extended_euclid(int a,int b){
    if(b==0){
        d = a;
        x = 1;
        y = 0;
        return;
    }else{
        extended_euclid(b,a%b);
        int  t = y;
        y = x - a/b*y;
        x = t;
        //cout<<x<<" "<<y<<endl;
        return;
    }
}
int main(){


    while(cin >> a >> b){

        extended_euclid(a,b);
        cout<<x<<" "<<y<<" "<<d<<endl;
    }

    return 0;
}



                
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值