Cod sursa(job #1762513)

Utilizator vlad.ulmeanu30Ulmeanu Vlad vlad.ulmeanu30 Data 23 septembrie 2016 17:34:56
Problema Algoritmul lui Euclid extins Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.81 kb
#include <stdio.h>

void calc ( long long a, long long b, long long &x, long long &y ) {
  if ( b == 0 ) {
    x = 1;
    y = 0;
  }
  else {
    long long x1, y1;

    calc ( b, a % b, x1, y1 );

    x = y1;
    y = x1 - y1 * ( a / b );
  }
}

int main () {
  FILE *fin, *fout;

  fin = fopen ( "euclid3.in", "r" );
  fout = fopen ( "euclid3.out", "w" );

  int n;

  fscanf ( fin, "%d", &n );

  int i;
  long long a, b, c, cmmdc;
  long long x, y;

  for ( i = 0; i < n; i++ ) {
    fscanf ( fin, "%lld%lld%lld", &a, &b, &c );
    calc ( a, b, x, y );

    cmmdc = a * x + b * y;

    if ( c % cmmdc == 0 )
      fprintf ( fout, "%lld %lld\n", x * c / cmmdc, y * c / cmmdc );
    else
      fprintf ( fout, "0 0\n" );
  }

  fclose ( fin );
  fclose ( fout );

  return 0;
}