Cod sursa(job #2875880)

Utilizator PatrascuAdrian1Patrascu Adrian Octavian PatrascuAdrian1 Data 22 martie 2022 15:29:13
Problema Algoritmul lui Euclid extins Scor 50
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.08 kb
#include <bits/stdc++.h>

using namespace std;

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

int gcd_facut_in_casa(int a, int b)
{
    int c;
    while(b)
    {
        c = b;
        b = a % b;
        a = c;
    }
    return a;
}

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

int main()
{
    int T, a, b, c, d;

    in >> T;
    while(T--)
    {
        in >> a >> b >> c;
        //d = __gcd(a,b);
        d = gcd_facut_in_casa(a,b);
        //out << d << '\n';
        if(c % d)
        {
            out << 0 << " " << 0 << '\n';
        }
        else
        {
            int e, x, y;
            euclid_extins(a, b, &e, &x, &y);
            //out << a << " " << b << " " << e << " " <<x << " " << y << '\n';
            out << x * c / d << " " << y * c / d << '\n';
        }
    }
    return 0;
}