Cod sursa(job #2096847)

Utilizator BarbumateiBarbu Matei Barbumatei Data 29 decembrie 2017 22:05:23
Problema Algoritmul lui Euclid extins Scor 100
Compilator c Status done
Runda Arhiva educationala Marime 0.68 kb
#include <stdio.h>
#include <stdlib.h>

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

int main() {
  FILE *fin, *fout;
  fin = fopen( "euclid3.in", "r" );
  fout = fopen( "euclid3.out", "w" );
  int t, a, b, c, d, x, y;
  fscanf( fin, "%d", &t );
  while ( t-- ) {
    fscanf( fin, "%d%d%d", &a, &b, &c );
    cmmdc( a, b, &d, &x, &y );
    if ( c % d )
      fprintf( fout, "0 0\n" );
    else {
      c /= d;
      fprintf( fout, "%d %d\n", x * c, y * c );
    }
  }
  fclose( fin );
  fclose( fout );
    return 0;
}