Cod sursa(job #2751348)

Utilizator cosminradu1760Cosmin-Andrei Radu cosminradu1760 Data 14 mai 2021 20:06:54
Problema Planeta Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.91 kb
#include<bits/stdc++.h>

using namespace std;

ifstream fin("planeta.in");
ofstream fout("planeta.out");

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

void bst(int n,long long pozitie,int x)
{
    long long suma = 0;
	int p = 0;

	if(n == 0)
        return;

	for(int i = 1;i <= n; ++i)
	{
		suma += D[n][i];

		if(suma >= pozitie)
            {
                p = i;
                break;
            }
	}

	if(p == 0)
        p = n + 1;

	suma -= D[n][p];
	pozitie -= suma;

	fout<<p + x<<" ";

	int lungime1 = p - 1, lungime2 = n - p;

	bst(lungime1, (pozitie - 1) / (S[lungime2]) + 1, x);
	bst(lungime2, (pozitie-1) % (S[lungime2]) + 1, x + p);


}

int main()
{
    fin>>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];
	}

	bst(N,K,0);

	return 0;
}