Cod sursa(job #333300)

Utilizator sapiensCernov Vladimir sapiens Data 22 iulie 2009 11:49:19
Problema Factorial Scor 100
Compilator fpc Status done
Runda Arhiva de probleme Marime 0.93 kb
Program fact;
 var f,g:text; np,p:longint;
 procedure initiere;
  begin
   assign (f,'fact.in'); reset (f);
   assign (g,'fact.out'); rewrite (g);
   readln (f,p);
   np:=0;
  end;
 procedure incheiere;
  begin
   close (f); close (g);
  end;
 function valoare (x:longint):longint;
  var y:longint;
  begin
   y:=0;
   while (x div 5)<>0 do begin
     y:=y+x div 5;
     x:=x div 5;
   end;
   valoare:=y;
  end;
 procedure calcul (x,y:longint);
  begin
   if y=x+1 then
     if (valoare (x)<>p) and (valoare (y)<>p) then writeln (g,-1) else begin
       if valoare (x)=p then writeln (g,x) else
         if valoare (y)=p then writeln (g,y);
     end
   else begin
     if (valoare (x)<=p) and (valoare ((x+y) div 2)>=p) then calcul (x,(x+y) div 2);
     if (valoare ((x+y) div 2)<p) and (valoare (y)>=p) then calcul ((x+y) div 2,y);
   end;
  end;
 begin
  initiere;
  calcul (1,500000000);
  incheiere;
 end.