Cod sursa(job #254702)

Utilizator mariusdrgdragus marius mariusdrg Data 7 februarie 2009 13:47:21
Problema Planeta Scor 100
Compilator cpp Status done
Runda Stelele Informaticii 2009, clasele 9-10, ziua 2 Marime 0.8 kb
#include<stdio.h>

const int maxn = 40;

int N;
long long K, S[maxn],D[maxn][maxn];

void print(int n,long long pos,int offset)
{
	if (n == 0) return ;
	long long sumcur = 0;	
	int poz = 0;
	for(int i = 1;i <= n; ++i)
	{
		sumcur += D[n][i];	
		if (sumcur >= pos) {poz = i;break;}
	}
	if (poz == 0) poz = n + 1;
	sumcur -= D[n][poz];
	pos -= sumcur;
	printf("%d ",poz + offset);
	int lung1 = poz - 1;
	int lung2 = n - poz;
	print(lung1,(pos - 1) / S[lung2] + 1,offset);
	print(lung2,(pos - 1) % S[lung2] + 1,offset + poz);
}

int main()
{
	freopen("planeta.in","r",stdin);
	freopen("planeta.out","w",stdout);
	scanf("%d %lld",&N,&K);
	S[0] = 1;
	for(int i = 1;i <= N; ++i)
	{

		for(int j = 1;j <= N; ++j)
			D[i][j] = S[j - 1] * S[i - j];
		for(int j = 1;j <= N; ++j)
			S[i] += D[i][j];
	}
	print(N,K,0);
	return 0;
}