若是凉夜已成梦

青春里 总有些事情要努力去做 总有些梦想要拼命去追。


  • 运维

  • 前端

  • 编程

  • 随笔

  • hust-oj

1544: Euclid problem

发表于 2017-10-06   |   分类于 HUSTOJ   |   阅读次数 1,259

题目描述

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 the corresponding X, Y, and D for a given A and B.

输入

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

输出

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 and | X| + | Y| is minimal.

样例输入

4 6
17 17


样例输出

-1 1 2
0 1 17


参考代码

#include"stdio.h"
#include"math.h"
long gcd(long p,long q,long *x,long *y) 
{
    long x1,y1;
    long g;
    if(q>p)
            return gcd(q,p,y,x);
    if(q==0) 
    {
        *x=1;
        *y=0;
        return p;
    }
    g=gcd(q,p%q,&x1,&y1);
    *x=y1;
    *y=(x1-floor(p/q)*y1);
    return g;
}
int main() 
{
    long a,b,x,y,c;
    while(scanf("%ld%ld",&a,&b)!=EOF) 
    {
        c=gcd(a,b,&x,&y);
        printf("%ld %ld %ldn",x,y,c);
    }
    return 0;
}

解析

暂无

hustoj

发表评论 取消回复

邮箱地址不会被公开。 必填项已用*标注

*
*


hoxis wechat
著作权归作者所有
站点更新说明
  • 文章目录
  • 站点概览
若是凉夜已成梦

若是凉夜已成梦

青春里 总有些事情要努力去做 总有些梦想要拼命去追。

1904 日志
6 分类
12 标签
RSS
weibo github twitter facebook

友情链接

原站点 Dreams孤独患者 Skip
© 2017 若是凉夜已成梦
Powered by WordPress | 已运行
Theme By NexT.Mist