Cod sursa(job #1235579)

Utilizator Corina1997Todoran Ana-Corina Corina1997 Data 29 septembrie 2014 23:57:52
Problema Algoritmul lui Euclid extins Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.73 kb
#include <fstream>
using namespace std;

ifstream is("euclid3.in");
ofstream os("euclid3.out");

int EDC( int a, int b, int &x, int &y );

int main()
{
    int t;
    is >> t;
    for ( int i = 1; i <= t; i++ )
    {
        int a, b, c, d;
        is >> a >> b >> c;
        int x, y;
        d = EDC(a, b, x, y);
        if(c % d)
            os << "0 0\n";
        else
            os << x * (c / d) << ' ' << y * (c / d) << '\n';
    }
    is.close();
    os.close();
    return 0;
}

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