Cod sursa(job #1613887)

Utilizator DoubleNyNinicu Cristian DoubleNy Data 25 februarie 2016 18:08:08
Problema Numere 2 Scor 60
Compilator fpc Status done
Runda Arhiva de probleme Marime 4.03 kb
type bignumbers=array[0..120] of integer;
var A,B,P,n,rezultat:qword;
    BigP,BigA,BigB,Big:bignumbers;
    numar:string;
    c:0..1;
    low,hight,mid:integer;
    BigR:boolean;


  //atribuire
  procedure atribuire(var bigN:bignumbers;r:integer);
   var aux:string;
       i,j:integer;
   begin
      fillchar(bign,sizeof(bign),0);
      str(r,aux);
      bign[0]:=length(aux);
      i:=1;
      j:=bign[0];
      repeat
           val(aux[j],bign[i]);
           inc(i);
           dec(j);
      until i>bign[0];

   end;

  //Bigpower
  procedure putere(biga,bigb:bignumbers; mid:integer; var big:bignumbers);
  var i,j,k:integer;
   begin
    for k:=1 to mid do
    begin
        i:=1;
        j:=1;
        for j:=1 to bigb[0] do
        for i:=1 to biga[0] do
        big[i+j-1]:=big[i+j-1]+biga[i]*bigb[j];
        for i:=1 to bigb[0]*biga[0] do
             if big[i]>9 then
              begin
                  big[i+1]:=big[i+1]+big[i] div 10;
                  big[i]:=big[i] mod 10;
              end;
       for i:=bigb[0]*biga[0]+5 downto 1 do if big[i]<>0 then begin big[0]:=i; break  end;
       fillchar(bigb,sizeof(bigb),0);
       for i:=0 to big[0]+1 do bigb[i]:=big[i];
       fillchar(big,sizeof(big),0);
    end;
    for i:=0 to bigb[0]+1 do big[i]:=bigb[i];
   end;

  //Relatii
  function smaller(A,B:bignumbers):boolean;
  var i,j:integer;
   begin
       smaller:=false;
       if a[0]>b[0] then exit
       else if a[0]<b[0] then smaller:=true
            else
               begin
                   for i:=a[0] downto 1 do
                    if a[i]>b[i] then exit
                    else smaller:=true;
               end;

   end;

  function bigger(A,B:bignumbers):boolean;
  var i,j:integer;
   begin
       bigger:=false;
       if a[0]<b[0] then exit
       else if a[0]>b[0] then bigger:=true
            else
               begin
                   for i:=a[0] downto 1 do
                    if a[i]>b[i] then begin bigger:=true; exit end
                    else bigger:=false;
               end;
   end;

  function ok(A,B:bignumbers):boolean;
  var i,j:integer;
  begin
       ok:=false;
       if a[0]<>b[0] then exit
       else
         begin
            for i:=a[0] downto 1 do
            if a[i]<>b[i] then begin ok:=false; exit end
            else ok:=true;
         end;
   end;
 //Relatii




 //Cautare
 procedure cautare();
 var i,j,pow:integer;
  begin
      //transcriere numar
      i:=1;
      j:=length(numar);
      BigP[0]:=length(numar);
      repeat
          val(numar[j],bigp[i],c);
          inc(i);
          dec(j);
      until i>Bigp[0];
      low:=1;
      hight:=335;
      Bigr:=false;
      pow:=2;
      mid:=1;
      repeat
           //mid:=low+(hight-low) div 2;
           atribuire(BigA,pow);
           fillchar(big,sizeof(big),0);
           putere(bigA,bigA,mid-1,big);
           if ok(big,bigp) then begin bigr:=true; break end;
          // if smaller(big,bigp) then begin low:=mid; continue end;
          // if (bigger(big,bigp)) then begin hight:=mid; continue end;
          // if hight-low<=1 then begin inc(pow); low:=1; hight:=335; end;
          if bigger(big,bigp) then
           begin
             inc(pow);
             mid:=1;
           end else inc(mid);
      until BigR;
  // for i:=bigp[0] downto 1 do write(bigp[i],' ');
  // writeln;
   writeln(output,pow); write(output,mid);

  end;


Begin
    assign(input,'numere2.in'); reset(input);
    assign(output,'numere2.out'); rewrite(output);
    readln(input,numar);
    if length(numar)<=1 then
    begin
    val(numar,p,c);
    a:=2;
    b:=1;
       repeat
            if exp(b*ln(a))>p then
             begin
                  inc(a);
                  b:=1;
             end else inc(b);
          rezultat:=round(exp(b*ln(a)));
       until rezultat=p;
    writeln(output,a);
    write(output,b);
    end else
    begin
        cautare();
    end;

    close(input);
    close(output);

End.