Cod sursa(job #1248082)

Utilizator gabrieligabrieli gabrieli Data 24 octombrie 2014 16:54:01
Problema Algoritmul lui Euclid extins Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.65 kb
#include <fstream>
#include <utility>

using namespace std;

int gcd_ext(int a, int b, int &x, int &y) {
    if (b == 0) {
        x = 1;
        y = 0;
        return a;
    }
    
    int d = gcd_ext(b, a%b, x, y);
    
    int t = x;
    x = y;
    y = t - (a / b) * y;
    
    return d;
}

int main() {
    ifstream fin("euclid3.in");
    ofstream fout("euclid3.out");
    
    int q, a, b, c, x, y, d;
    
    for(fin >> q; q; --q) {
        fin >> a >> b >> c;
        d = gcd_ext(a, b, x, y);
        
        if (c % d == 0) 
            fout << x * (c / d) << ' ' << y * (c / d) << '\n';
        else
            fout << "0 0\n";
    }
    
    return 0;
}