`

UVA 10104 Euclid Problem

阅读更多

        新手请进:扩展欧几里德入门

/*
*   直接Egcd就得出|x|+|y|最小的解
*   不知道为什么可以这样,我觉得分4种情况讨论的做法更靠谱些
*/
#include <iostream>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
using namespace std;
#define LL long long
#define M 105
#define inf 0x3fffffff

LL Egcd (LL a, LL b, LL &x, LL &y)
{
    if (b == 0)
    {
        x = 1; y = 0;
        return a;
    }
    LL tp, d;
    d = Egcd (b, a%b, x, y);
    tp = x;
    x = y;
    y = tp-a/b*y;
    return d;
}

int main()
{
    LL a, b, x, y, d;
    while (cin >> a >> b)
    {
        d = Egcd(a, b, x ,y);
        cout << x << ' ' << y << ' ' << d << endl;
    }
    return 0;
}

 

1
1
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics