Cod sursa(job #1932634)

Utilizator dr55Dan Rusu dr55 Data 19 martie 2017 22:29:07
Problema Algoritmul lui Euclid extins Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.7 kb
#include <fstream>
#include <string>

const std::string NO_SOL = "0 0\n";

int gcd(int a, int b, int& x, int& y)
{
    if ( b == 0 )
    {
        x = 1;
        y = 0;
        return a;
    }

    int x0, y0, d;
    d = gcd(b, a % b, x0, y0);

    x = x0;
    y = y0 - (a/b) * y0;
    return d;
}

int main()
{
    std::ifstream fin("euclid3.in");
    std::ofstream fout("euclid3.out");

    int t;
    for ( fin >> t; t; t--)
    {
        int a, b, c;
        fin >> a >> b >> c;
        int d, x, y;
        d = gcd(a,b,x,y);
        if ( c % d )
        {
            fout << NO_SOL;
        }
        else
        {
            fout << x * (c/d) << ' ' << y * (c/d) << '\n';
        }
    }

    return 0;
}