Cod sursa(job #945848)

Utilizator sebii_cSebastian Claici sebii_c Data 3 mai 2013 01:39:04
Problema Algoritmul lui Euclid extins Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.75 kb
#include <fstream>

void extended_euclid(int a, int b, int *d, int *x, int *y)
{
    if (b == 0) {
        *d = a;
        *y = 0;
        *x = 1;
    } else {
        int x0, y0;
        extended_euclid(b, a % b, d, &x0, &y0);
        *x = y0;
        *y = x0 - a / b * y0;
    }
}

// Test code
int main()
{
    std::ifstream fin("euclid3.in");
    std::ofstream fout("euclid3.out");

    int T;
    fin >> T;

    while (T--) {
        int a, b, c;
        fin >> a >> b >> c;

        int x, y, d;
        extended_euclid(a, b, &d, &x, &y);
        if (c % d != 0) {
            fout << 0 << " " << 0 << "\n";
        } else {
            x = x * (c / d);
            y = y * (c / d);
            fout << x << " " << y << "\n";
        }
    }

    return 0;
}