Cod sursa(job #525198)

Utilizator MihaicorneliuMihai Pojar Mihaicorneliu Data 24 ianuarie 2011 17:15:41
Problema Factorial Scor 100
Compilator fpc Status done
Runda Arhiva de probleme Marime 0.76 kb
program factorial;
var p,gasit:longint;
    f,g:text;

function nrdezero(x:longint):longint;
var c,d:longint;
begin
  d:=0;
  c:=x;
  repeat
    c:=c div 5;
    d:=d+c
  until c=0;
  nrdezero:=d
end;

procedure caut(s,d,nr:longint);
var m:longint;
begin
  m:=nrdezero((s+d)div 2);
  if m=nr then
    begin
      gasit:=(s+d) div 2;
      while not(gasit mod 5 = 0) do
        gasit:=gasit-1
    end
  else
    if s<>d then
      if m>nr then
        caut (s,(s+d) div 2,nr)
      else
        caut ((s+d) div 2 + 1,d,nr)
end;

begin
  assign(f,'fact.in');
  reset(f);
  readln(f,p);
  close(f);
  gasit:=-1;
  caut(1,5*p,p);
  assign(g,'fact.out');
  rewrite(g);
  if p=0 then
    gasit:=1;
  writeln(g,gasit);
  close(g);
end.