Cod sursa(job #61484)

Utilizator bogdan88Bogdan Popescu bogdan88 Data 19 mai 2007 17:09:22
Problema Factorial Scor 90
Compilator fpc Status done
Runda Arhiva de probleme Marime 1.11 kb
var sol:int64;
    s,ct,rez,val,st,dr,mijloc:int64;
    i:longint;
    fi,fo:text;
    uc:byte;
begin
     assign(fi,'fact.in'); reset(fi);
     assign(fo,'fact.out'); rewrite(fo);
     read(fi,val);
     st:=1; dr:=3000000000;
     while st<=dr do
        begin
             sol:=0;
             mijloc:=(st+dr) div 2;
             ct:=5;
             while ct<=mijloc do
                begin
                     sol:=sol+trunc(mijloc/ct);
                     ct:=ct*5;
                end;
             if sol=val then begin rez:=mijloc; break; end
                else
                    if sol>val then dr:=mijloc-1
                               else st:=mijloc+1;
        end;
     if rez=0 then writeln(fo,'-1')
        else
            begin
                 if rez<5 then begin rez:=1; exit; end;
                 if (rez>4)and(rez<10) then begin rez:=2; exit; end;
                 uc:=rez mod 10;
                 if uc<5 then rez:=(rez div 10)*10
                         else rez:=(rez div 10)*10+5;
            end;
     writeln(fo,rez);
     close(fi);
     close(fo);
end.