Cod sursa(job #2195470)

Utilizator gabrielxCojocaru Gabriel-Codrin gabrielx Data 16 aprilie 2018 14:26:49
Problema Algoritmul lui Euclid extins Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.42 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <map>
#include <unordered_map>
#include <queue>
#include <deque>
#include <cmath>
#include <algorithm>

using namespace std;

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

void executeExtendedEuclideanAlgorithm(int a, int b, int &x, int &y, int &gcd) {
	int x1 = 1, y1 = 0;
	int x2 = 0, y2 = 1;
	int xAux, yAux;
	int r, q;

	while (a % b != 0) {
		q = a / b;
		r = a % b;

		xAux = x2;
		yAux = y2;

		x2 = x1 - q * x2;
		y2 = y1 - q * y2;

		x1 = xAux;
		y1 = yAux;

		a = b;
		b = r;
	}

	gcd = b;
	x = x2;
	y = y2;
}

int main() {
	cin.sync_with_stdio(false);
	cout.sync_with_stdio(false);
	
	int T, a, b, c;
	int x, y, gcd;

	fin >> T;

	while (T--) {
		fin >> a >> b >> c;

		if (a == 0 && b == 0) {
			fout << 0 << ' ' << 0 << '\n';
			continue;
		}

		if (a == 0) {
			if (c % b == 0) {
				fout << 0 << ' ' << c / b << '\n';
			}
			else {
				fout << 0 << ' ' << 0 << '\n';
			}

			continue;
		}

		if (b == 0) {
			if (c % a == 0) {
				fout << c / a << ' ' << 0 << '\n';
			}
			else {
				fout << 0 << ' ' << 0 << '\n';
			}

			continue;
		}

		executeExtendedEuclideanAlgorithm(a, b, x, y, gcd);

		if (c % gcd == 0) {
			fout << (c / gcd) * x << ' ' << (c / gcd) * y << '\n';
		}
		else {
			fout << 0 << ' ' << 0 << '\n';
		}
	}
	
	return 0;
}