Cod sursa(job #139059)

Utilizator Pepelea_FlaviuFlaviu Pepelea Pepelea_Flaviu Data 19 februarie 2008 17:52:23
Problema Factorial Scor 90
Compilator fpc Status done
Runda Arhiva de probleme Marime 0.93 kb
var fi,fo:text;
    p,x,rez:int64;
function calculate(n:int64):int64;
var i,ct:int64;
begin
  i:=5; ct:=0;
  while i<n do
    begin
      ct:=ct + (n div i);
      i:=i*5;
    end;
  calculate:=ct;
end;
function binars(st,dr:int64):int64;
var mij,aux,aux2:int64;
begin
  while st<=dr do
    begin
      mij:=(st+dr) shr 1;
      aux:=calculate(mij);
      if aux=p then
        begin
          binars:=mij;
          exit;
        end
      else
       if aux>p then dr:=mij+1
                else st:=mij-1;
    end;
  binars:=-1;
end;
begin
  assign(fi,'fact.in'); reset(fi);
  assign(fo,'fact.out'); rewrite(fo);
  read(fi,p);
  if p=0 then writeln(fo,1)
    else
     begin
       rez:=binars(1,2000000000);
       if rez mod 10<5 then rez:=(rez div 10)*10
                       else rez:=(rez div 10)*10+5;
       if rez=0 then rez:=1;
       writeln(fo,rez);
     end;
  close(fi);
  close(fo);
end.