Cod sursa(job #2825211)

Utilizator rares89_Dumitriu Rares rares89_ Data 4 ianuarie 2022 12:25:23
Problema Algoritmul lui Euclid extins Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.91 kb
#include <fstream>

using namespace std;

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

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

inline int gcd( int A, int B, int &X, int &Y )
{
	if (B == 0)
	{
		X = 1;
		Y = 0;
		return A;
	}
 
	int X0, Y0, D;
	D = gcd( B, A % B, X0, Y0 );
	
	X = Y0;
	Y = X0 - (A / B) * Y0;
	return D;
}

int t, a, b, c;

int main() {
    fin >> t;
    while(t > 0) {
        fin >> a >> b >> c;
        int x, y;
        int d = gcd(a, b, x, y);
        if(c % d == 0) {
            fout << (x * (c / d) ) << " " << (y * (c / d) ) << "\n";
        } else {
            fout << "0 0\n";
        }
        t--;
    }
    return 0;
}