ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

Discussion of Problem 1013. K-based Numbers. Version 3

Filippov Nickolas SSAU#2's AC program is HERE!
Posted by Nickolas 14 Feb 2003 19:48
program kbasednumbers;
 const max=500;
       osn=100000000;
 type big=array[0..max] of longint;
  var i:integer;
      a,b,c:big;
      n,k:word;

  procedure sumbig(var a,b,c:big);
   var k,i:word;
    begin
      FillChar(c,sizeof(c),0);
      if a[0]>=b[0] then k:=a[0] else k:=b[0];
      for i:=1 to k do begin
        c[i+1]:=(a[i]+b[i]+c[i]) div osn;
        c[i]:=(a[i]+b[i]+c[i]) mod osn;
      end;
      if c[k+1]<>0 then c[0]:=k+1 else c[0]:=k;
    end;

  procedure multbignum(var a:big; number:word);
   var k,i:word;
       um,um_temp:longint;
    begin
      um:=0;
      for i:=1 to a[0] do begin
        um_temp:=(um+a[i]*number) div osn;
        a[i]:=(um+a[i]*number) mod osn;
        um:=um_temp;
      end;
      if um<>0 then begin inc(a[0]); a[a[0]]:=um; end;
    end;

  procedure printbig(var a:big);
   var nosn:word;
       s:string;
    begin
      str(osn div 10,s);
      nosn:=length(s);
      write(a[a[0]]);
      for i:=a[0]-1 downto 1 do begin
        str(a[i],s);
        while length(s)<nosn do s:='0'+s;
        write(s);
      end;
    end;

begin
 read(n);
 read(k);
 a[0]:=1;
 a[1]:=1;
 b[0]:=1;
 b[1]:=k-1;
 for i:=2 to n do begin
   sumbig(a,b,c);
   multbignum(c,k-1);
   a:=b;
   b:=c;
 end;
 printbig(b);
end.