Cod sursa(job #2886733)

Utilizator toma_ariciuAriciu Toma toma_ariciu Data 8 aprilie 2022 11:41:46
Problema Algoritmul lui Euclid extins Scor 50
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.95 kb
#include <iostream>
#include <fstream>

using namespace std;

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

void euclid_extins(int a, int b, int &x, int &y)
{
    if(b == 0)
    {
        x = 1;
        y = 0;
        return;
    }
    euclid_extins(b, a % b, x, y);
    int xprim = x, yprim = y;
    x = yprim;
    y = xprim - (a / b) * yprim;
}

void solve()
{
    bool swapped = 0;
    int a, b, c = 0, d, x = 0, y = 0;
    fin >> a >> b >> d;
    if(a < b)
    {
        swap(a, b);
        swapped = 1;
    }
    euclid_extins(a, b, x, y);
    c = a * x + b * y;
    if(d % c != 0)
    {
        fout << "0 0\n";
        return;
    }
    x = x * d / c;
    y = y * d / c;
    if(swapped)
        swap(x, y);
    fout << x << ' ' << y << '\n';
}

int main()
{
    int nr_teste;
    fin >> nr_teste;
    while(nr_teste--)
        solve();
    return 0;
}