Cod sursa(job #520878)

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

using namespace std;

int x, y, z,
    a, b, d;
vector<int> list;
vector<int> :: reverse_iterator rit; 

void euclid_cmmdc()
{
    list.clear();
    while ((x != 0)&&(y != 0))
    {
        if (x > y)
        {
            list.push_back(x/y);
            x = x % y;
        }
        else
        {
            list.push_back(y/x);
            y = y % x;
        }
    }

    d = x + y;

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

        for (rit = list.rbegin(); rit != list.rend(); ++rit)
        {
            x = b;
            y = a - *rit * b;
            a = x;
            b = y;
        }

        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;
}