Cod sursa(job #2900531)

Utilizator toma_ariciuAriciu Toma toma_ariciu Data 11 mai 2022 06:47:15
Problema Algoritmul lui Euclid extins Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.1 kb
#include <fstream>

using namespace std;

#define int long long

const string filename = "euclid3";
ifstream fin(filename + ".in");
ofstream fout(filename + ".out");

/// x0 * b + y0 * (a - (a/b) * b) = c
/// x * a + y * b = c
/// x * a + y * b = y0 * a + x0 * b - y0 * (a / b) * b;

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

signed main()
{
    int nr_teste;
    fin >> nr_teste;
    while(nr_teste--)
    {
        int a, b, c, d, x = 0, y = 0;
        bool swapped = 0;
        fin >> a >> b >> d;
        if(a > b)
        {
            swap(a, b);
            swapped = 1;
        }
        big_euclid(a, b, x, y);
        c = a * x + b * y;
        if(d % c != 0)
            fout << "0 0\n";
        else
        {
            if(swapped)
                swap(x, y);
            x *= d/c;
            y *= d/c;
            fout << x << ' ' << y << '\n';
        }
    }
    return 0;
}