Cod sursa(job #3151202)

Utilizator Chris_BlackBlaga Cristian Chris_Black Data 20 septembrie 2023 09:43:19
Problema Algoritmul lui Euclid extins Scor 40
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.85 kb
#include <bits/stdc++.h>

using namespace std;


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

    int x0, y0;

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

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

    return aux;
}

void solve()
{
    int a, b, c, x, y;
    cin >> a >> b >> c;

    int cmmdc;
    euclid_extins(a, b, cmmdc, x, y);

    if(c % cmmdc != 0)
    {
        cout << "0 0\n";
        return;
    }

    a = a * c / cmmdc;
    b = b * c / cmmdc;

    euclid_extins(a, b, c, x, y);

    cout << x * c / cmmdc << ' ' << y * c / cmmdc << '\n';
}

int main()
{
    freopen("euclid3.in", "r", stdin);
    freopen("euclid3.out", "w", stdout);

    int t;
    cin >> t;
    while(t --)
        solve();
    return 0;
}