Cod sursa(job #2643518)

Utilizator LolluckestarNastasa-Baras Luca Lolluckestar Data 20 august 2020 10:07:25
Problema Algoritmul lui Euclid extins Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.69 kb
#include <iostream>
#include <fstream>
using namespace std;

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

void extendedGCD(long a, long b, long* x, long* y, long* r) 
{
	if (b == 0) 
	{
		*x = 1;
		*y = 0;
		*r = a;
	}
	else 
	{
		long x0, y0;
		extendedGCD(b, a % b, &x0, &y0, r);
		*x = y0;
		*y = x0 - (a / b) * y0;
	}
}


int main() 
{
	int T;
	fin >> T;

	while (T--)
	{
		long a, b, c, x, y, gcd;
		fin >> a >> b >> c;

		extendedGCD(a, b, &x, &y, &gcd);
		if (c % gcd != 0) 
		{
			fout << 0 << " " << 0 << '\n';
			continue;
		}

		fout << (x * c) / gcd << " " << (y * c) / gcd << '\n';
	}

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

	return 0;
}