Pagini recente » Cod sursa (job #50258) | Cod sursa (job #2964994) | Cod sursa (job #620775) | Cod sursa (job #853743) | Cod sursa (job #448390)
Cod sursa(job #448390)
#include <algorithm>
using namespace std;
#define DIM 35
long long best[DIM];
long long k;
int v[DIM];
int n,m;
void init ()
{
int i,j;
best[0]=best[1]=1;
for (i=2; i<=n; ++i)
for (j=0; j<i; ++j)
best[i]+=best[j]*best[i-j-1];
}
void solve (int st,int dr,long long poz)
{
long long cnt,nrc,mij1,mij2;
int i,ind;
cnt=0;
for (i=1; i<=dr-st+1; ++i)
{
cnt+=best[i-1]*best[dr-st-i+1];
if (cnt>=poz)
{
cnt-=best[i-1]*best[dr-st-i+1];
ind=i;
break;
}
}
if (cnt<poz)
poz-=cnt;
nrc=best[dr-st-ind+1];
mij1=poz/nrc;
mij2=poz-mij1*nrc;
if (poz%nrc)
++mij1;
else
mij2+=nrc;
v[++m]=st+ind-1;
if (ind>1)
solve (st,st+ind-2,mij1);
if (st+ind<=dr)
solve (st+ind,dr,mij2);
}
void print ()
{
int i;
for (i=1; i<=n; ++i)
printf ("%d ",v[i]);
}
int main ()
{
freopen ("planeta.in","r",stdin);
freopen ("planeta.out","w",stdout);
scanf ("%d%lld",&n,&k);
init ();
solve (1,n,k);
print ();
return 0;
}