Cod sursa(job #3293002)

Utilizator StefantimStefan Timisescu Stefantim Data 9 aprilie 2025 23:33:37
Problema Algoritmul lui Euclid extins Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.84 kb
#include <bits/stdc++.h>
using namespace std;
ifstream fin("euclid3.in");
ofstream fout("euclid3.out");

/*
    a * x + b * y = d
    b * x0 + (a % b) * y0 = d
    a % b = a - b * c;

    b * x0 + (a - b * c) * y0 = a * x + b * y
    b * (x0 - y - c * y0) = a * (x - y0)
    daca x - y0 = 0 atunci y = x0 - (a/b) * y0
*/
void euclid(int a, int b, int &d, int &x, int &y)
{
    if (b == 0)
    {
        d = a;
        x = 1;
        y = 0;
    }
    else
    {
        int x0, y0;
        euclid(b, a % b, d, x0, y0);
        x = y0;
        y = x0 - (a / b) * y0;
    }
}
int main()
{
    int t, a, b, c, d, x, y;
    fin >> t;
    while (t--)
    {
        fin >> a >> b >> c;
        euclid(a, b, d, x, y);
        if(c % d != 0)
            fout << "0 0\n";
        else
            fout << x * (c / d) << " " << y * (c / d) << "\n";
    }
    return 0;
}