Cod sursa(job #520899)

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

using namespace std;

int x, y, z,
    a, b, d,
    rev;

int stck[1024];
int size = 0;

int pop()
{
    size--;
    return stck[size];
}

void push(int x)
{
    stck[size] = x;
    size++;
}

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

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

    d = x + y;

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

        while(size)
        {
            x = b;
            y = a - pop() * b;
            a = x;
            b = y;
        }

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

int main()
{
    int nr;

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

    scanf("%d", &nr);

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

    return 0;
}