Cod sursa(job #191158)

Utilizator FllorynMitu Florin Danut Flloryn Data 25 mai 2008 15:35:52
Problema Factorial Scor 25
Compilator fpc Status done
Runda Arhiva de probleme Marime 2.15 kb
program psacal;
var f,g:text; p:int64;
    x,aux,nr,d,k:int64;
    u,v:array[1..10] of int64;

     procedure test;
        begin
         k:=p;
         d:=0;
        while p<>0 do
               begin
                 d:=d+1;
                 p:=p div 10;
               end;
         p:=k;
         k:=u[d+1];
         x:=v[d+1];
               repeat
                  aux:=x;
                 while (aux mod 5=0) and (aux<>0) do
                 begin
                    k:=k-1;
                    aux:=aux div 5;
                 end;
                 x:=x-5;
              until (k=p) or (k<p);

      if k=p then write(g,x)
          else write(g,-1);
    end;

    procedure test1;
     begin
               repeat
                  aux:=x;
                 while (aux mod 5=0) and (aux<>0) do
                 begin
                    k:=k-1;
                    aux:=aux div 5;
                 end;
                 x:=x-5;
              until (k=p) or (k<p);

      if k=p then write(g,x)
          else write(g,-1);
     end;
begin
 assign(f,'fact.in'); reset(f);
 assign(g,'fact.out'); rewrite(g);
 read(f,p);
if p<=100000 then
 begin
 u[1]:=2; v[1]:=10;
 u[2]:=24; v[2]:=100;
 u[3]:=249; v[3]:=1000;
 u[4]:=2499; v[4]:=10000;
 u[5]:=24999; v[5]:=100000;
 if p=0 then write(g,1)
 else  test;
 end
    else
    begin
     if (p>100000) and (p<=500000) then
                  begin
                      k:=124999;
                      x:=500000;
                      test1;
                  end;
     if (p>500000) and (p<=1000000) then
                  begin
                      k:=249998;
                      x:=1000000;
                      test1;
                  end;
    if (p>1000000) and (p<=5000000) then
                  begin
                      k:=1249998;
                      x:=5000000;
                      test1;
                  end;
    if (p>5000000) and (p<=1000000000) then
                  begin
                      k:=2499999;
                      x:=1000000000;
                      test1;
                  end;

    end;
 close(f);
 close(g);
end.