Cod sursa(job #520885)

Utilizator cprodescuProdescu Corneliu-Claudiu cprodescu Data 10 ianuarie 2011 18:28:13
Problema Algoritmul lui Euclid extins Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.05 kb
#include <cstdio>
#include <stack>

using namespace std;

int x, y, z,
    a, b, d,
    rev;
stack<int> stck;

void euclid_cmmdc()
{
    if (x > y)
        rev = 0;
    else
        rev = 1;

    while ((x != 0)&&(y != 0))
    {
        if (x > y)
        {
            stck.push(x/y);
            x = x % y;
        }
        else
        {
            stck.push(y/x);
            y = y % x;
        }
    }

    d = x + y;

    if (z % d == 0)
    {
        z = z / d;
        a = 1;
        b = 1;

        while(!stck.empty())
        {
            x = b;
            y = a - stck.top() * b;
            a = x;
            b = y;
            stck.pop();
        }

        if (rev)
            printf("%d %d\n", b*z, a*z);
        else
            printf("%d %d\n", a*z, b*z);
    }
    else
    {
        printf("0 0\n");
    }
}

int main()
{
    int n;

    freopen("euclid3.in", "r", stdin);
    freopen("euclid3.out", "w", stdout);

    scanf("%d", &n);

    while(n--)
    {
        scanf("%d %d %d", &x, &y, &z);
        euclid_cmmdc();
    }

    return 0;
}