Cod sursa(job #58230)

Utilizator cezar305Mr. Noname cezar305 Data 4 mai 2007 18:48:42
Problema GFact Scor 30
Compilator fpc Status done
Runda Arhiva de probleme Marime 1.38 kb
var f1,f2:text;
    a,b,d,x,m,s1,s2,nr,max,p,s,r:longint;
    i,j,n,q,nr1,h:longint;

procedure putere(d,x:longint);
begin
        p:=1;
        s:=0;
        while p*d<=x do
        begin
                p:=p*d;
                s:=s+x div p;
        end;
end;

procedure search(li,ls:qword);
begin
        m:=(li+ls) div 2;
        putere(a,m*a);
        s1:=s;
        putere(a,(m-1)*a);
        s2:=s;
        if (s1>=b)and(s2<b) then nr1:=m*a
                else if li<ls then if s1>=b then search(li,m-1)
                        else search(m+1,ls);
end;

begin
        assign(f1,'gfact.in');
        reset(f1);
        assign(f2,'gfact.out');
        rewrite(f2);
        read(f1,h,q);
        nr:=2;j:=0;
        r:=h;
        for i:=2 to r do
        begin
                if h mod i=0 then
                begin
                        b:=0;
                        a:=i;
                        while h mod i=0 do
                        begin
                                inc(b);
                                h:=h div i;
                        end;
                        b:=b*q;
                        n:=j;
                        nr1:=0;
                        search(1,b);
                        if nr1>max then max:=nr1;
                end;
        end;
        writeln(f2,max);
        close(f1);
        close(f2);
end.