Cod sursa(job #1727798)

Utilizator AndreiFlorescuAndrei Florescu AndreiFlorescu Data 11 iulie 2016 16:53:54
Problema Algoritmul lui Euclid extins Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.82 kb
#include <fstream>

using namespace std;

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

int main() {
    ifstream file_in ("euclid3.in");
    ofstream file_out ("euclid3.out");

    int a, b, c, t;
    int d, x, y;

    // Citirea datelor
    file_in >> t;

    // Calcularea solutiei
    for (; t; --t) {
        file_in >> a >> b >> c;
        d = euclidExt (a, b, x, y);

        // Afisarea solutiei

        if (c % d) {
            file_out << "0 0\n";
        } else {
            file_out << x * (c / d) << " " << y * (c / d) << "\n";
        }
    }

    return 0;
}