Cod sursa(job #2886737)

Utilizator toma_ariciuAriciu Toma toma_ariciu Data 8 aprilie 2022 11:43:25
Problema Algoritmul lui Euclid extins Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.99 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, long long &x, long long &y)
{
    if(b == 0)
    {
        x = 1;
        y = 0;
        return;
    }
    euclid_extins(b, a % b, x, y);
    long long xprim = x, yprim = y;
    x = yprim;
    y = xprim - (a / b) * yprim;
}

void solve()
{
    bool swapped = 0;
    int a, b, c = 0, d;
    long long x = 0, y = 0;
    fin >> a >> b >> d;
    if(a < b)
    {
        swap(a, b);
        swapped = 1;
    }
    euclid_extins(a, b, x, y);
    c = 1LL * (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;
}