SPBINARY - Xâu Nhị Phân

Tác giả: flashmt

Ngôn ngữ: Pascal

const fi='';
      fo='';
      maxn=601;
      base=1000000000;
      dg=9;
type bignum=array[0..21] of longint;
var f:array[0..maxn] of bignum;
    n,k:longint;

procedure rf;
begin
     read(n,k);
end;

procedure plus(var a:bignum;b,c:bignum);
var i,mem,max:longint;
begin
     mem:=0;
     if b[0]>c[0] then max:=b[0] else max:=c[0];
     for i:=1 to max do
     begin
          a[i]:=b[i]+c[i]+mem;
          if a[i]>=base then
          begin
               a[i]:=a[i]-base;
               mem:=1;
          end
          else mem:=0;
     end;
     if mem>0 then
     begin
          inc(max);
          a[max]:=1;
     end;
     a[0]:=max;
end;

procedure pr;
var i,j:longint;
begin
     f[1,0]:=1; f[1,1]:=2;
     for i:=2 to n do
         if i<=k then plus(f[i],f[i-1],f[i-1])
         else
             for j:=1 to k do plus(f[i],f[i],f[i-j]);
end;

procedure wf;
var i,j,l:longint; s:string;
begin
     for i:=f[n,0] downto 1 do
     begin
          if i<f[n,0] then
          begin
               str(f[n,i],s);
               l:=length(s);
               for j:=l+1 to dg do write(0);
          end;
          write(f[n,i]);
     end;
end;

begin
     assign(input,fi); reset(input);
     assign(output,fo); rewrite(output);
     rf;
     pr;
     wf;
     close(input); close(output);
end.

Download