Cod sursa(job #2329527)

Utilizator AndreiVisoiuAndrei Visoiu AndreiVisoiu Data 26 ianuarie 2019 21:28:33
Problema Planeta Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.01 kb
#include <cstdio>

using namespace std;

FILE *in = fopen("planeta.in", "r"),
     *out = fopen("planeta.out", "w");
const int NMAX = 32;
unsigned long long K, m = 1,
          A[NMAX];
int N, x = 1,
    S[NMAX];

void dp() {
    A[0] = 1;

    for(int i = 1; i <= N; i++)
        for(int j = 0; j < i; j++)
            A[i] += A[j] * A[i-j-1];
}

void bestChoice(int p, int u) {
    if(p > u) return;
    if(p == u) {
        S[p] = x;
        return;
    }
    int cnt = u-p+1;
    long long sum = 0;
    int i = 1;
    for(i; i <= cnt; i++) {
        if(sum + 1LL * A[i-1] * A[cnt-i] * m >= K) break;
        sum += 1LL * A[i-1] * A[cnt-i] * m;
    }
    K -= sum;

    S[p] = x+i-1;

    m *= A[cnt - i];
    bestChoice(p+1, p+i-1);
    m /= A[cnt - i];

    x += i;
    bestChoice(p+i, u);
    x -= i;
}

int main()
{
    fscanf(in, "%i %lld", &N, &K);

    dp();

    bestChoice(1, N);

    for(int i = 1; i <= N; i++)
        fprintf(out, "%i ", S[i]);
    return 0;
}