Cod sursa(job #781505)

Utilizator pongraczlajosLajos Pongracz pongraczlajos Data 24 august 2012 16:08:55
Problema Algoritmul lui Euclid extins Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.83 kb
#include <fstream>
using namespace std;

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

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