Cod sursa(job #1528091)

Utilizator whoiscrisCristian Plop whoiscris Data 19 noiembrie 2015 01:12:13
Problema Algoritmul lui Euclid extins Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.72 kb
#include <iostream>
#include <fstream>
#include <algorithm>
using namespace std;

typedef long long int ll;

#define REP(i, a, b) \
	for (int i=int(a); i<=int(b); ++i)

ifstream f("euclid3.in");
ofstream g("euclid3.out");


void gcd(ll a, ll b, ll *d) {
	if (b == 0)
		*d = a;
	else
		gcd(b, a%b, d);
}

void egcd(ll a, ll b, ll *d, ll *x, ll *y) {
	if (b == 0) {
		*d = a;
		*x = 1;
		*y = 0;
	}
	else {
		ll x0, y0;
		egcd(b, a%b, d, &x0, &y0);
		*x = y0;
		*y = x0 - (a/b) * y0;
	}
}


ll a, b, c, x, y, d;

int main () {

	int t;
	f >> t;
	while (t--) {
		f >> a >> b >> c;
		gcd(a, b, &d);
		if (c % d)
			x = y = 0;
		else {
			a *= (c/d);
			b *= (c/d);
			egcd(a, b, &d, &x, &y);
		}
		g << x << " " << y << endl;
	}

	return 0;
}