Cod sursa(job #438197)

Utilizator gramatovici_paulGramatovici Paul gramatovici_paul Data 10 aprilie 2010 15:57:40
Problema GFact Scor 90
Compilator fpc Status done
Runda Arhiva de probleme Marime 1.35 kb
var pi,pp:array[1..1000] of longint;
    p,q,j,x,n,pas:int64;
procedure divizori(p,q:int64);
var putere,i:int64;
    k:int64;
begin
  k:=trunc(sqrt(p));
  i:=2;
  while i<=k do
      begin
        putere:=0;
        while p mod i=0 do
           begin
             inc(putere);
             p:=p div i;
           end;
        if putere>0 then
            begin
                inc(j);
                pi[j]:=i;
                pp[j]:=putere*q;
            end;
        inc(i);
      end;
  if p>1 then
     begin
       inc(j);
       pi[j]:=p;
       pp[j]:=q;
     end;
end;
function putere(x,y:int64):int64;
var xy:int64;
begin
  xy:=0;
  while(x<>0) do
    begin
      xy:=xy+x div y;
      x:=x div y;
    end;
  putere:=xy;
end;

function verif(n:int64):boolean;
var i:longint;
begin
  verif:=true;
  for i:=1 to j do
    if putere(n,pi[i])<pp[i] then
       begin
         verif:=false;
         break;
       end;
end;

function caut:int64;
var x:int64;
    pas:int64;
begin
  x:=0;pas:=1 shl 30;
  while pas<>0 do
       begin
         if not verif(x+pas) then x:=x+pas;
         pas:=pas shr 1;
       end;
  caut:=x;
end;

begin
  assign(input,'gfact.in');
  assign(output,'gfact.out');
  reset(input);
  rewrite(output);
  readln(p,q);
  divizori(p,q);
  writeln(caut+1);
  close(output);
end.