Cod sursa(job #3317833)

Utilizator nopreanOprean Natasha noprean Data 25 octombrie 2025 15:19:34
Problema Algoritmul lui Euclid extins Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.99 kb
#include <bits/stdc++.h>

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

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

int main()
{
    int n, a, b, c;
    fin >> n;
    for (int i = 1; i <= n; i++)
    {
        int cInitial;
        fin >> a >> b >> c;
        cInitial = c;
        int x, y;
        euclid(a, x, b, y, c);
        /// la final, c va contine cmmdc(a,b)
        /// noi vom avea doua numere x si y astfel incat a * x + b * y = cmmdc(a,b)
        if (cInitial % c != 0)
        {
            /// numerele nu au niste coeficienti care sa dea rezultatul cInitial
            fout << 0 << ' ' << 0 << '\n';
        }
        else
            fout << 1LL * x * cInitial / c << " " << 1LL * y * cInitial / c << '\n';
    }
    return 0;
}