Cod sursa(job #3263239)

Utilizator CezarTDTodirisca Cezar CezarTD Data 13 decembrie 2024 22:47:15
Problema Algoritmul lui Euclid extins Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.71 kb
#include <iostream>
#include <fstream>

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

		return d;
	}
}

int main()
{
	int n;
	// a, b and c are the coefficients in the followin equation: ax + by = c
	// d is the gcd(a, b)
	int a, b, c, d;

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

	fin >> n;
	for (; n; n--)
	{
		fin >> a >> b >> c;

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

		if (c % d != 0)
		{
			fout << "0 0\n";
			continue;
		}
		
		
		fout << x * (c / d) << " " << y * (c / d) << '\n';
	}

	return 0;
}