Cod sursa(job #265913)

Utilizator mariusdrgdragus marius mariusdrg Data 24 februarie 2009 19:01:48
Problema Planeta Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.98 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,(long long)(pos - 1) / S[lung2] + 1,offset);
	print(lung2,(long long)(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] = (long long)S[j - 1] * S[i - j];
//			if (D[i][j] < 0) printf("caca\n");
		}
		for(int j = 1;j <= N; ++j)
			S[i] += D[i][j];
//		if (S[i] < 0) printf("caca\n");
	}
	print(N,K,0);
	printf("\n");
	return 0;
}