Cod sursa(job #1427344)

Utilizator codrut_grosuGrosu Codrut-Cristian codrut_grosu Data 1 mai 2015 23:19:37
Problema Algoritmul lui Euclid extins Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.03 kb
# include <iostream>
# include <fstream>

void read_data ( std :: istream & , long int & ) ;
void euclid ( long int , long int , long int & , long int & , long int & ) ;


int main () {

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

	long int T ;

	read_data ( f , T ) ;
	for ( int i = 0 ; i < T ; i ++ ) {
	
		long int a ;
		long int b ;
		long int c ;
		long int x ;
		long int y ;
		long int d ;
		
		read_data ( f , a ) ;
		read_data ( f , b ) ;
		read_data ( f , c ) ;
		euclid ( a , b , d , x , y ) ;
		if ( c % d ) {
		
			g << 0 << ' ' << 0 << '\n' ;
		
		} else {
		
			g << x * ( c / d ) << ' ' << y * (c / d ) << '\n' ;
		
		}
	
	}

	f.close () ;
	g.close () ;
	return 0 ;

}

void euclid ( long int a , long int b , long int & d , long int & x , long int & y ) {

	if ( b == 0 ) {
	
		d = a ;
		x = 1 ;
		y = 0 ;
	
	} else {
	
		long int x0 , y0 ;
		
		euclid ( b , a % b , d , x0 , y0 ) ;
		
		x = y0 ;
		y = x0 - (a/b) * y0 ;
	
	}


}


void read_data ( std :: istream & f , long int & x ) {

	f >> x ;

}