Cod sursa(job #2897187)

Utilizator RobertuRobert Udrea Robertu Data 2 mai 2022 20:52:45
Problema Planeta Scor 10
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.62 kb
#include <fstream>
#include <iostream>
std::fstream fin("planeta.in");
std::ofstream fout("planeta.out");

const int dim = 32;
int n;
long long  catalan[dim], k;

void rez(int st, int dr, long long kk) {
    int i;
    for(i = st; catalan[i - st] * catalan[dr - i] <= kk && i <= dr; i++) kk -= catalan[i - st] * catalan[dr - i];
    fout << i << ' ';

    if(i > st) rez(st, i - 1, kk);
    if(i < dr) rez(i + 1, dr, kk);
}

int main() {
    fin >> n >> k;
    catalan[0] = catalan[1] = 1;
    for(int i = 2; i <= n; i++) for(int j = 0; j < i; j++) catalan[i] += catalan[j] * catalan[i - j - 1];
    rez(1, n, --k);

    return 0;
}