Cod sursa(job #58325)

Utilizator cezar305Mr. Noname cezar305 Data 5 mai 2007 12:26:56
Problema GFact Scor 20
Compilator fpc Status done
Runda Arhiva de probleme Marime 1.75 kb
var f1,f2:text;
    a,b,s1,s2,p,s,r:longint;
    i,j,n,q,h,x:longint;
    m,max,nr1:qword;

procedure power(d,x:qword);
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;
        power(a,m*a);
        s1:=s;
        power(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;

procedure prog;
begin
        b:=0;
        a:=i;
        while h mod i=0 do
        begin
                inc(b);
                h:=h div i;
        end;
        b:=b*q;
        nr1:=0;
        search(1,b);
        if nr1>max then max:=nr1;
end;

procedure prime(x:longint);
var i:longint;
begin
        p:=0;
        for i:=2 to trunc(sqrt(x)) do
                if x mod i=0 then
                begin
                        p:=1;
                        break;
                end;
end;

begin
        assign(f1,'gfact.in');
        reset(f1);
        assign(f2,'gfact.out');
        rewrite(f2);
        read(f1,h,q);
        i:=1;
        while i<=trunc(sqrt(h)) do
        begin
                x:=i;
                if h mod i=0 then
                begin
                        prime(i);
                        if (p=0)and(i<>1) then prog;
                        i:=h div i;
                        prime(i);
                        if (p=0)and(i<>1) then prog;
                end;
                if x<3 then i:=x+1
                       else i:=x+2;
        end;
        writeln(f2,max);
        close(f1);
        close(f2);
end.