Cod sursa(job #13397)

Utilizator runnaway90Oprescu Radu Constantin runnaway90 Data 6 februarie 2007 14:43:01
Problema Factorial Scor 50
Compilator fpc Status done
Runda Arhiva de probleme Marime 1.25 kb

var f,g:text;
        a,b,x,m,fact,suma,p,r:longint;
        ok:boolean;
function zerouri(w:longint):longint;
var fact,max:longint;
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:=100000000;
        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
                a:=(a+b) div 2;
             end
             else
             if suma >p then
                b:=(a+b) div 2;
        end;
        if a=b then
                write(g,-1);
        close(g);
end.