Cod sursa(job #2366860)

Utilizator serbancoroiuSerban Ionut Coroiu serbancoroiu Data 4 martie 2019 22:31:14
Problema Algoritmul lui Euclid extins Scor 100
Compilator c-64 Status done
Runda Arhiva educationala Marime 0.66 kb
#include <stdlib.h>
#include <stdio.h>

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

int main(){

	FILE *ptr, *ptrO;
	ptr = fopen("euclid3.in", "r");
	ptrO = fopen("euclid3.out", "w");
	long n = 0, a=0, b=0, c=0, d=0, x=0, y=0;
	fscanf(ptr, "%ld", &n);
	for(int i=0;i<n;i++){
		fscanf(ptr, "%ld %ld %ld\n", &a, &b, &c);
		d = gcd(a, b, &x, &y);
		if(c%d == 0)
			fprintf(ptrO, "%ld %ld\n", x*c/d, y*c/d);
		else
			fprintf(ptrO, "0 0\n");
	}	
	fclose(ptr);
	fclose(ptrO);
	return 0;
}