Cod sursa(job #2900512)

Utilizator stalecuAlecu Stefan-Iulian stalecu Data 10 mai 2022 23:20:28
Problema Algoritmul lui Euclid extins Scor 70
Compilator fpc Status done
Runda Arhiva educationala Marime 1 kb
program euclid3;

{$MODE objfpc}{$H+}{$J-}
uses sysutils;

procedure Euclid3(a, b: longint; out d: longint; out x: longint; out y: longint);
var
  newX, newY: longint;
begin
  if b = 0 then
  begin
    d := a;
    x := 1;
    y := 0;
  end
  else
  begin
  Euclid3(b, a mod b, d, x, y);
  newX := y;
  newY := x - y * (a div b);
  x := newX;
  y := newY;
  end;
end;

const
  C_IN_FNAME = 'euclid3.in';
  C_OUT_FNAME = 'euclid3.out';
var
  fin, fout: text;
  n, a, b, c, d, x, y : longint;
begin
  try
  AssignFile(fin, C_IN_FNAME);
  AssignFile(fout, C_OUT_FNAME);
  Reset(fin);
  Readln(fin, n);
  Rewrite(fout);
  while not Eof(fin) do
  begin
    Readln(fin, a, b, c);
    Euclid3(a, b, d, x, y);
    if c mod d <> 0 then
      Writeln(fout, '0 0')
    else
    begin

      Writeln(fout, IntToStr(x * (c div d)), ' ', IntToStr(y * (c div d)));
    end;
  end;
  CloseFile(fin);
  CloseFile(fout);
  except
    on E:Exception do
      Writeln('File ', C_IN_FNAME, ' could not be created because: ', E.Message);
  end;
end.