Cod sursa(job #2171522)

Utilizator RazorBestPricop Razvan Marius RazorBest Data 15 martie 2018 12:37:49
Problema Algoritmul lui Euclid extins Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.89 kb
#include <fstream>
using namespace std;

ifstream fin("euclid3.in");
ofstream fout("euclid3.out");

void eec(int a, int b, int c, int &x, int &y)
{
    if (a % b == c)
    {
        x = 1;
        y = - (a / b);
        return;
    }
    if (a % b == 0)
    {
        x = 0;
        y = 0;
        return;
    }

    int x2;
    eec(b, a % b, c, x2, x);
    y = x2 - x * (a / b);
}

int main()
{
    int T, a, b, c, x, y;

    fin >> T;
    while (T--)
    {
        bool invers = false;

        fin >> a >> b >> c;
        if (a < b)
        {
            swap(a, b);
            invers = true;
        }
        eec(a, b, c % a, x, y);

        if (x != 0 && y != 0)
        {
            x += c / a;
            if (invers)
                swap(x, y);
            fout << x  << ' ' << y << '\n';
        }
        else
            fout << "0 0\n";
    }
}