Cod sursa(job #615515)

Utilizator ctlin04UAIC.VlasCatalin ctlin04 Data 9 octombrie 2011 21:41:29
Problema GFact Scor 100
Compilator fpc Status done
Runda Arhiva de probleme Marime 1.53 kb
Program gfact;
type tip=record
     fact,put:int64;
     end;
 var p:array [1..1000] of tip;
    a,q,max,b:int64;
    i,n:longint;
    fi,fo:text;
procedure descompune();
  var j,k,s:int64;
 begin
 i:=2; k:=0;
  while i<=trunc(sqrt(a)) do
   if a mod i<>0 then inc(i)
    else begin
        inc(k);
         p[k].fact:=i; s:=0;
            repeat
             inc(s);
              a:=a div i;
            until (a=1) or (a mod i<>0);
          p[k].put:=s*q;
          inc(i);
          end;
 if a>1 then begin p[k+1].fact:=a; p[k+1].put:=q; n:=k+1; end
 else n:=k;
end;
function control(h:int64):boolean;
var k,j:int64;
begin
 j:=p[i].fact; k:=0;
 while trunc(h/j)>0 do begin
                     k:=k+trunc(h/j);
                     j:=j*p[i].fact;
                     end;
 if k>=p[i].put then control:=true
  else control:=false;
end;
procedure cauta(l,r:int64);
 var mid:int64;
begin
 mid:=(l+r) div 2;
if l<r then begin
if control(mid) then cauta(l,mid-1)
                 else cauta(mid+1,r);
                end
 else begin
       while control(mid)=false do inc(mid);
        while control(mid)=true do dec(mid);
        b:=mid+1;
          end;
end;
procedure get_fact();
begin
 for i:=1 to n do begin
   cauta(1,p[i].put*p[i].fact);
    if b>max then max:=b;
      end;
end;
begin
 assign(fi,'gfact.in');
  assign(fo,'gfact.out');
 reset(fi); rewrite(fo);
 readln(fi,a,q);
 if a=1 then write(fo,'1')
 else begin descompune();
   get_fact();
 write(fo,max);
  end;
close(fo);
end.