Cod sursa(job #3358200)

Utilizator Stamate_DavidStamate David Stamate_David Data 15 iunie 2026 10:50:13
Problema Algoritmul lui Euclid extins Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.94 kb
#include <fstream>
using namespace std;

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

// gasim x si y astfel incat a*x + b*y = d unde d = cmmdc(a, b)
long long euclid(long long a, long long b, long long &x, long long &y)
{
    if (b == 0)
    {
        x = 1;
        y = 0;
        return a;
    }

    long long x1, y1;
    long long d = euclid(b, a % b, x1, y1);

    x = y1;
    y = x1 - (a / b) * y1;

    return d;
}

int main()
{
    int t;
    fin >> t;

    for (int i = 0; i < t; i++)
    {
        long long a, b, c, x, y;
        fin >> a >> b >> c;

        long long d = euclid(a, b, x, y);

        if (d < 0)
        {
            d = -d;
            x = -x;
            y = -y;
        }
        
        if (d == 0 || c % d != 0)
        {
            fout << 0 << " " << 0 << "\n";
        }
        else
        {
            long long k = c / d;
            fout << x * k << " " << y * k << "\n";
        }
    }

    return 0;
}