Cod sursa(job #68386)

Utilizator MDanFMI - Dan Moldovan MDan Data 27 iunie 2007 19:00:35
Problema Factorial Scor 10
Compilator fpc Status done
Runda Arhiva de probleme Marime 2.19 kb
const c1=0;
      c2=0;
      c3=0;
      c4=0;
      c5=0;
      c6=0;
      c7=0;
      c8=0;
      c9=0;
      c10=0;
      c11=0;
var p,max,min,aux,auxaux:longint;
    f,g:text;
    ok:boolean;
    a:array [1..3] of longint;
function nrzero (z:longint):longint;
var nrz,coef:longint;
begin
if z>24
then
    begin
    coef:=1;
    nrz:=c1;
    end;
if z>124
then
    begin
    coef:=coef+1;
    nrz:=c2;
    end;
if z>624
then
    begin
    coef:=coef+1;
    nrz:=c3;
    end;
if z>3124
then
    begin
    coef:=coef+1;
    nrz:=c4;
    end;
if z>15624
then
    begin
    coef:=coef+1;
    nrz:=c5;
    end;
if z>78124
then
    begin
    coef:=coef+1;
    nrz:=c6;
    end;
if z>390624
then
    begin
    coef:=coef+1;
    nrz:=c7;
    end;
if z>1953124
then
    begin
    coef:=coef+1;
    nrz:=c8;
    end;
if z>9765624
then
    begin
    coef:=coef+1;
    nrz:=c9;
    end;
if z>48828124
then
    begin
    coef:=coef+1;
    nrz:=c10;
    end;
if z>244140624
then
    begin
    coef:=coef+1;
    nrz:=c11;
    end;
z:=z-nrz;
nrz:=nrz+(coef*(z div 5));
end;




begin
assign (f,'fact.in');
assign (g,'fact.out');
reset (f);
rewrite (g);
readln (f,p);
min:=1;
max:=maxlongint;
ok:=true;
while ok do
           begin
           auxaux:=nrzero(trunc((min/2)+(max/2)));
           if p<auxaux
           then
               begin
               max:=trunc((min/2)+(max/2));
               a[3]:=a[2];
               a[2]:=a[1];
               a[1]:=max;
               if a[1]=a[3]
               then
                   ok:=false;
               end;
           if p>auxaux
           then
               begin
               min:=trunc((min/2)+(max/2));
               a[3]:=a[2];
               a[2]:=a[1];
               a[1]:=min;
               if a[1]=a[3]
               then
                   ok:=false;
               end;
           if p=auxaux
           then
               begin
               writeln (g,(min/2)+(max/2));
               ok:=false;
               auxaux:=-1;
               end;
           end;

if auxaux<>-1
then
    writeln (g,'-1');




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