Cod sursa(job #195613)

Utilizator theratmantheratman theratman Data 20 iunie 2008 10:59:47
Problema Factorial Scor 10
Compilator fpc Status done
Runda Arhiva de probleme Marime 0.64 kb
var f,g:text;
    a,b,p,c:longint;
function zero(c:longint):longint;
 var x,p,d,nr:longint;
 begin
 dec(c,c mod 5);
 x:=c div 5;
 p:=25;
 d:=p*5;
 nr:=3;
 while p<=c do begin
  if d=p then begin
     inc(x,nr);
     d:=d*5;
     inc(nr);
  end
   else inc(x,1);
  inc(p,25);
 end;
 zero:=x;
 end;
begin
assign(f,'fact.in');reset(f);
assign(g,'fact.out');rewrite(g);
read(f,p);
a:=1;
b:=1000000000;
while (a<=b) do begin
 c:=(a+b) div 2;
 if zero(c)<p then a:=c+1;
 if zero(c)>p then b:=c-1;
 if zero(c)=p then begin
  writeln(g,c-c mod 5);
  break;
 end;
end;
if a>b then writeln(g,'-1');
close(f);
close(g);
end.