Cod sursa(job #373766)

Utilizator DarkByteVictor Pop DarkByte Data 15 decembrie 2009 00:20:07
Problema Factorial Scor 0
Compilator fpc Status done
Runda Arhiva de probleme Marime 1.24 kb
program Untitled;

function GetFactZero(X: Longint): Longint;
var div5, c: integer;
begin
  div5 := 0;
  c := -1;

  while X > 4 do
    begin
      div5 := div5 + (X div 5);
      x := x div 5;
      inc(c);
    end;

  if X = 0 then
    div5 := div5 + c;

  Result := (X div 25) + div5;
end;

var x, y, xy, lr, P: longint; // last result
    fil: Text;
begin
  x := 1;
  y := 100000000;

  Assign(fil, 'fact.in');
  Reset(fil);
  Read(fil, P);
  Close(fil);
//  P := 2; // read from file
  repeat
    xy := (x + y) div 2;
    lr := GetFactZero(xy);
    if (lr = P) or ((xy = x) or (xy = y))
      then Break
      else
        if lr < P
          then
            begin
              x := xy;
            end
          else
            begin
              y := xy;
            end
  until False;

  if (lr = P)
    then
      begin
        xy := xy - (xy mod 5);
        Assign(fil, 'fact.out');
        Rewrite(fil);
        Write(fil, P);
        Close(fil);
//        write('Result = ', xy)
      end
    else
      begin
        Assign(fil, 'D:\fact\fact.out');
        Rewrite(fil);
        Write(fil, '-1');
        Close(fil);
//        write('That''s all, folks !');
      end;

  readln;
end.