Cod sursa(job #298784)

Utilizator AndreiDumaAndrei Duma AndreiDuma Data 6 aprilie 2009 13:09:46
Problema Factorial Scor 100
Compilator fpc Status done
Runda Arhiva de probleme Marime 0.66 kb
var p,a,b,c:longint;
    ok:boolean;

function zero( x:longint ):longint;
var i:longint;
begin
	i:=5; zero:=0;
	while i<=x do
	begin
		zero :=zero+x div i;
		i:=i*5;
	end;
end;


begin
assign(input,'fact.in');reset(input);
assign(output,'fact.out');rewrite(output);
readln(p);

b:=2000000000;

while not ok do
begin
        c:=(a+b) div 2;
        if zero(c)=p then ok:=true
        else
                if zero(c)<p then a:=c+1
                             else b:=c-1;
        if a=b then begin c:=-1; ok := true; end;
end;

if p=0 then writeln(1) else if c<>-1 then writeln(c-c mod 5) else writeln(c);
close(input); close(output);

end.