Pagini recente » Cod sursa (job #1361590) | Cod sursa (job #684573) | Cod sursa (job #47874) | Cod sursa (job #945262) | Cod sursa (job #13405)
Cod sursa(job #13405)
var f,g:text;
a,b,x,m,fact,suma,p,r:int64;
ok:boolean;
function zerouri(w:int64):int64;
var fact,max:int64;
begin
fact:=5;max:=0;
while w div fact<>0 do
begin
max:=max+w div fact;
fact:=fact*5;
end;
zerouri:=max;
end;
begin
assign(f,'fact.in');
reset(f);
read(f,p);
close(f);
b:=5000000000;
a:=0;
ok:=true;
suma:=0;
assign(g,'fact.out');
rewrite(g);
while (a<=b)and(ok) do
begin
x:=(a+b) div 2;
m:=x;
suma:=zerouri(x);
if suma=p then
begin
while (suma=p)and(m>0) do
begin
m:=m-1;
r:=m;
ok:=false;
suma:=zerouri(r);
end;
write(g,m+1);
end
else
if suma<p then
begin
if a=b-1 then
ok:=false;
a:=(a+b) div 2;
end
else
if suma >p then
begin
if a=b-1 then
ok:=false;
b:=(a+b) div 2;
end;
end;
if (a=b)or((a=b-1)and(ok=false)) then
write(g,-1);
close(g);
end.