Cod sursa(job #2643517)

Utilizator LolluckestarNastasa-Baras Luca Lolluckestar Data 20 august 2020 10:05:59
Problema Algoritmul lui Euclid extins Scor 0
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;
	cin >> T;

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

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

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

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

	return 0;
}