Cod sursa(job #2921453)

Utilizator tzancauraganuTzanca Uraganu tzancauraganu Data 31 august 2022 11:50:26
Problema Algoritmul lui Euclid extins Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.87 kb
#include <fstream>

using namespace std;

void solve(int a, int b, int& x, int& y, int& d) {
    if (a < b) {
        solve(b, a, y, x, d);
        return;
    }
    if (b == 0) {
        x = 1;
        y = 1;
        d = a;
        return;
    }
    int r = a % b;
    if (r == 0) {
        x = 0;
        y = 1;
        d = b;
        return;
    }
    solve(b, r, x, y, d);

    int aux = y;
    y = x - (a / b) * y;
    x = aux;
    return;
}

int main() {
    ifstream f("euclid3.in");
    ofstream g("euclid3.out");

    int n;
    f >> n;

    for (int i = 0; i < n;i++) {
        int a, b, c;
        int x, y, d;
        f >> a >> b >> c;
        solve(a, b, x, y, d);
        if (d == 0 || c % d != 0)
            g << "0 0\n";
        else {
            int k = c / d;
            g << x * k << ' ' << y * k << '\n';
        }
    }

    f.close();
    g.close();
    return 0;
}