Cod sursa(job #2982934)

Utilizator BeneIonut2208Bene Ionut-Matei BeneIonut2208 Data 21 februarie 2023 10:18:44
Problema Algoritmul lui Euclid extins Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.85 kb
#include <iostream>
#include <fstream>

using namespace std;

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

int euclid(int a, int b, int &x, int &y)
{
    int x0, y0, x1, y1;
    x0 = 1;
    x1 = 0;
    y0 = 0;
    y1 = 1;
    while(b)
    {
        int r = a % b;
        int q = a / b;
        x = x0 - q * x1;
        y = y0 - q * y1;
        a = b;
        b = r;
        x0 = x1;
        x1 = x;
        y0 = y1;
        y1 = y;
    }
    x = x0;
    y = y0;
    return a;
}

int main()
{
    int t, a, b, c;
    fin >> t;
    for(int i = 1; i <= t; i++)
    {
        fin >> a >> b >> c;
        int x, y;
        int d = euclid(a, b, x, y);
        if(c % d == 0)
            fout << x * (c / d) << ' ' << y * (c / d) << '\n';
        else
            fout << 0 << ' ' << 0 << '\n';
    }
    return 0;
}