uva 10104 (扩展欧几里德算法)

    今天粗略学习了扩展欧几里德算法。

  设a,b,c为任意整数,g=gcd(a,b),方程 ax+by=g的一组解是(x1,y1),则当c是g的倍数时,ax+by=c的一组解是(x1*c/g,y1*c/g);当c不是g的倍数时无整数解。

UVA 10104:

he 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) and X<=Y (secondarily).

Sample Input

4 6
17 17

Sample Output

-1 1 2
0 1 17


代码:

#include<cstdio>
void gcd(int a,int b,int &d,int &x,int &y)
{
    if(b==0)
    {
        d=a;
        x=1;y=0;
    }
    else
    {
        gcd(b,a%b,d,y,x);
        y-=x*(a/b);

    }
}

using namespace std;
int main()
{


    int a,b,d,x,y;
    while(scanf("%d%d",&a,&b)!=EOF)
    {
         gcd(a,b,d,x,y);
         printf("%d %d %d\n",x,y,d);
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值