Cod sursa(job #1919962)

Utilizator blatulInstitutul de Arta Gastronomica blatul Data 9 martie 2017 21:46:12
Problema Algoritmul lui Euclid extins Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.88 kb
#include <iostream>

using namespace std;

int Gcd(int a, int b, int& x, int& y) {
    if(b == 0) {
        x = 1;
        y = 0;
        return a;
    }
    
    int aux_x, aux_y;
    int r = Gcd(b, a % b, aux_x, aux_y);
    // aux_x * b + aux_y * (a - a / b * b) = r 
    // aux_b * b + aux_y * a - aux_y * (a / b) * b 
    // aux_y * a + b * (aux_x + aux_y * (a / b))
    
    x = aux_y;
    y = aux_x + aux_y * (a / b);
    return r;
}

int main() {
    #ifdef INFOARENA
    freopen("euclid3.in", "r", stdin);
    freopen("euclid3.out", "w", stdout);
    #endif
    
    int T;
    cin >> T;
    while(T--) {
        int a, b, c;
        cin >> a >> b >> c;
        int x, y;
        int d = Gcd(a, b, x, y);
        
        if(c % d)
            cout << "0 0\n";
        else {
            x *= c / d;
            y *= c / d;
            cout << x << ' ' << y << '\n';
        }
    }
    
}