Cod sursa(job #85681)

Utilizator savimSerban Andrei Stan savim Data 22 septembrie 2007 12:35:49
Problema Factorial Scor 95
Compilator fpc Status done
Runda Arhiva de probleme Marime 1.29 kb
var f1,f2:text;
    q,c,cz,s,p:longint;
begin
assign(f1,'fact.in');
assign(f2,'fact.out');
reset(f1);
rewrite(f2);
readln(f1,p);
if p=0 then begin
             writeln(f2,1);
             close(f1);
             close(f2);
             halt;
            end;
s:=0;cz:=0;
repeat
 s:=s+3125;
 cz:=cz+776;
 c:=s;
 while (c mod 5=0) and (c>=1) do
  begin
   cz:=cz+1;
   c:=c div 5;
  end;
until cz>=p;
if cz=p then writeln(f2,s)
        else
         begin
          q:=s;
          c:=s;
          while (c mod 5=0) and (c>=1) do
           begin
            cz:=cz-1;
            c:=c div 5;
           end;
          repeat
           s:=s-5;
           c:=s;
           while (c mod 5=0) and (c>=1) do
            begin
             cz:=cz-1;
             c:=c div 5;
            end;
           if cz<p then begin
                         writeln(f2,'-1');
                         close(f1);
                         close(f2);
                         halt;
                        end;
           if cz=p then begin
                         writeln(f2,s-5);
                         close(f1);
                         close(f2);
                         halt;
                        end;
          until s=q-3125;
         end;
close(f1);
close(f2);
end.