Cod sursa(job #3135668)

Utilizator sumithesumSumurduc Alexandru sumithesum Data 3 iunie 2023 23:18:18
Problema Planeta Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.85 kb
#include <iostream>
#include <vector>
#include <fstream>
using namespace std;

ifstream f("planeta.in");
ofstream g("planeta.out");

vector<long long> a;

void rezolvare(int x, int n, long long k)
{
    if (x > n)
        return;

    int i = x;
    while (a[i - x] * a[n - i] <= k && i <= n)
    {
        k = k - a[i - x] * a[n - i];
        i++;
    }

    g << i << " ";

    if (x < i)
        rezolvare(x, i - 1, k / a[n - i]);
    if (i < n)
        rezolvare(i + 1, n, k % a[n - i]);
}

int main()
{
    int n;
    long long k;
    f >> n >> k;

    a.resize(n + 1);
    a[0] = 1;

    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= i; j++)
        {
            a[i] += a[i - j] * a[j - 1];
        }
    }

    k--;
    rezolvare(1, n, k);

    f.close();
    g.close();
    return 0;
}