Cod sursa(job #287530)

Utilizator b_ady20Branescu Adrian b_ady20 Data 24 martie 2009 22:23:40
Problema Factorial Scor 100
Compilator fpc Status done
Runda Arhiva de probleme Marime 0.91 kb
var a,b,stop,c,e,n,p:int64;
function f(c:int64):int64;
var fact:int64;
begin
fact:=1;
e:=0;
while c div fact<>0 do
begin
fact:=fact*5;
if c div fact=0 then
break;
e:=e+c div fact;
end;
f:=e;
end;
begin
assign (input,'fact.in');
reset (input);
read (input,p);
close (input);
if p=0 then
begin
assign (output,'fact.out');
rewrite (output);
write (output,1);
close (output);
halt;
end
else
if p<=4 then
begin
assign (output,'fact.out');
rewrite (output);
write (output,p*5);
close (output);
halt;
end;
a:=0; b:=p*5; stop:=0;
while a<=b do
begin
c:=(a+b) div 2;
if f(c)<p then
a:=c+1
else
if f(c)>p then
b:=c-1
else
begin
stop:=p; a:=b+1;
end;
end;
assign (output,'fact.out');
rewrite (output);
if stop<>0 then
if c mod 10=5 then
write (output,c)
else if c mod 10>5 then write (output,c-(c mod 10 -5))
else write (output,c-c mod 10)
else
write (output,-1);
close (output);
end.