TAPN - TAPN
Tác giả: flashmt
Ngôn ngữ: Pascal
const maxn=31;
var n,t,i:longint;
re,m:int64;
d:array[1..31] of longint;
c,a:array[0..31,-1..31] of int64;
procedure init;
var i,j:longint;
begin
fillchar(a,sizeof(a),0);
for i:=1 to maxn do
begin
a[i,0]:=1;
a[i,i]:=1;
end;
for i:=2 to maxn do
for j:=1 to i-1 do
a[i,j]:=a[i-1,j]+a[i-1,j-1];
fillchar(c,sizeof(c),0);
for i:=1 to maxn do
begin
c[i,0]:=1;
for j:=1 to i do
c[i,j]:=c[i,j-1]+a[i,j];
end;
end;
procedure find;
var i,pos,j:longint;
begin
fillchar(d,sizeof(d),0); pos:=0;
if m=1 then exit;
if m=c[n,n] then
begin
for i:=1 to n do d[i]:=1;
exit;
end;
for i:=0 to n-1 do
if m>c[n,i] then pos:=i else break;
m:=c[n,pos+1]-m+1;
inc(pos);
for i:=n-1 downto 1 do
begin
if m>a[i,pos] then
begin
d[n-i]:=1;
m:=m-a[i,pos];
dec(pos);
end
else d[n-i]:=0;
end;
d[n]:=pos;
end;
begin
init;
repeat
readln(n,m);
find;
re:=1;
for i:=1 to n do
if d[i]=0 then re:=re*2+1
else re:=re*3+1;
writeln(re);
until eof;
end.