Cod sursa(job #52052)

Utilizator M@2Te4iMatei Misarca M@2Te4i Data 17 aprilie 2007 18:09:50
Problema Factorial Scor 45
Compilator fpc Status done
Runda Arhiva de probleme Marime 1.26 kb
var a,z,w,p:int64;
    li,lf:int64;
    //a,z:int64;

procedure cautare_bin(li,lf:longint);
var i:int64;
begin

a:=(li+lf) div 2;
i:=5;
w:=0;
while a div i>0 do
      begin
      w:=w+a div i;
      i:=i*5;
      end;
end;

procedure cautare;
var gasit:boolean;
begin
li:=1;
lf:=100000000;
gasit:=false;
if p=0 then
   a:=1
   else begin
        while li<=lf do
              begin
              cautare_bin(li,lf);
              if w=p then
                 begin
                 if not gasit then
                    z:=a
                    else begin
                         if a<z then z:=a;
                         end;
                 gasit:=true;
                 end;
                 if w<p then
                         begin
                         li:=a+1;
                         end
                      else begin
                           lf:=a-1;
                           end;
              end;
        if not gasit then
           a:=-1;
        end;
end;

procedure scriere;
begin
assign(output,'fact.out');
rewrite(output);
write(a);
close(output);
end;

procedure citire;
begin
assign(input,'fact.in');
reset(input);
read(p);
close(input);
end;

begin
citire;
cautare;
scriere;
end.