Cod sursa(job #2606357)

Utilizator Victor2006Nicola Victor-Teodor Victor2006 Data 27 aprilie 2020 16:15:43
Problema Algoritmul lui Euclid extins Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.91 kb
#include <iostream>
#include <fstream>

using namespace std;

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

void euclid_extins( int a, int b, int& x, int& y, int& d ) {
	if ( b == 0 ) {
		d = a;
		x = 1;
		y = 0;
		return;
	}
	int xx, yy, q = a / b;
	euclid_extins( b, a % b, xx, yy, d );
	x = yy;
	y = xx - yy * q;
}

int cmmdc( int a, int b ) {
	if ( b == 0 )
		return a;
	return cmmdc( b, a % b );
}

int main() {
    int a, b, c, t, i;
    fin >> t;
    for ( i = 0; i < t; i ++ ) {
        fin >> a >> b >> c;
        int x, y, d = cmmdc( a, b );
        if ( c % d == 0 ) { // exista x si y
            cout << "c: " << c << "\t" << "d: " << d << "\n";
            euclid_extins( a, b, x, y, d );
            x *= c / d;
            y *= c / d;
        }
        else {
            x = y = 0;
        }
        fout << x << " " << y << "\n";
    }
    return 0;
}