Cod sursa(job #2580522)

Utilizator PetrescuAlexandru Petrescu Petrescu Data 13 martie 2020 18:30:45
Problema Algoritmul lui Euclid extins Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.91 kb
#include <bits/stdc++.h>

using namespace std;

int solve(int a, int b, int &x, int &y)
{
    if(!b)
    {
        x = 1;
        y = 0;

        return a;
    }

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

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

    return d;
}

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

    ios::sync_with_stdio(false);
    fin.tie(0);
    fout.tie(0);

    int n;

    fin >> n;

    for(int i = 1; i <= n; i++)
    {
        int a, b, z;

        fin >> a >> b >> z;

        int x, y;
        int d = solve(a, b, x, y);

        if(z % d)
            fout << "0 0" << '\n';
        else
        {
            cout << z << " " << d << '\n';
            int bias = z / d;

            fout << x * bias << " " << y * bias << '\n';
        }
    }

    fin.close();
    fout.close();

    return 0;
}