Cod sursa(job #51311)

Utilizator taloibogdanTaloi Bogdan Cristian taloibogdan Data 11 aprilie 2007 10:04:33
Problema Factorial Scor 100
Compilator fpc Status done
Runda Arhiva de probleme Marime 0.65 kb
Program factorial;
Var f:text;
    p,a,b,c,z:longint;
function zero(c:longint):longint;
var j,zer:longint;
begin
 zer:=0;
 j:=1;
 repeat
  j:=j*5;
  zer:=zer+(c div j);
 until(j*5>c);
 zero:=zer;
end;
Begin
  assign(f,'fact.in');
  reset(f);
  readln(f,p);
  close(f);
  if p<>0 then
  begin
  a:=1;
  b:=1000000000;
  repeat
   c:=a+((b-a) div 2);
   z:=zero(c);
   if z<p then a:=c+1;
   if z>p then b:=c-1;
  until((z=p) or (a>b))
  end;
  assign(f,'fact.out');
  rewrite(f);
  if p=0 then Writeln(f,'1')
         else
  begin
  if z=p then Writeln(f,(c div 5)*5);
  if a>b then Writeln(f,'-1');
  end;
  close(f);
End.