Cod sursa(job #58477)

Utilizator cezar305Mr. Noname cezar305 Data 5 mai 2007 22:52:34
Problema GFact Scor 95
Compilator fpc Status done
Runda Arhiva de probleme Marime 2.45 kb
var f1,f2:text;
    a,b,s1,s2,p,s,r:qword;
    j,q,x,n:qword;
    m,max,nr1:qword;
    i,h,u:longint;
    y:array[1..100000] of longint;

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:=y[i];
        while h mod y[i]=0 do
        begin
                inc(b);
                h:=h div y[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;r:=h;j:=0;
        while i<=trunc(sqrt(r)) do
        begin
                if h mod i=0 then
                begin
                        prime(i);
                        if p=0 then
                        begin
                                if i<>1 then
                                begin
                                        inc(j);
                                        y[j]:=i;
                                end;
                                if h div i<>i then
                                begin
                                        prime(h div i);
                                        if p=0 then
                                        begin
                                                inc(j);
                                                y[j]:=h div i;
                                        end;
                                end;
                        end;
                end;
                if i<3 then i:=i+1
                       else i:=i+2;
        end;
        for i:=1 to j do
        begin
                prog;
        end;
        writeln(f2,max);
        close(f1);
        close(f2);
end.