简单题求调 悬关
查看原帖
简单题求调 悬关
974277
水星湖psgqwq楼主2024/10/2 23:20
#include <bits/stdc++.h>
#define int long long
using namespace std;

const int N = 20;
int n, h[N], c[N], m;
void dfs(int x, int y) {
    int cnt = 0;
    for(int i = x - 1; i >= 0; i--) {
        cnt += c[x - 1 - i] * c[i];
        if(cnt >= y) {
            cnt -= c[x - 1 - i] * c[i];
            int rk = y - cnt; 
            int p = (rk - 1) / c[x - i - 1];
            if(x - 1 - i) {
                cout << "(";
                dfs(x - 1 - i, rk - p * c[x - i - 1]);
                cout << ")";
            }
            cout << "X";
            if(i) {
                cout << "(";
                dfs(i, p + 1);
                cout << ")";
            }
            return;
        }
    }
}

signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    cin >> n; h[0] = h[1] = 1;
    if(n == 1) {
        cout << "X";
        return 0;
    }
    for(int i = 2; i <= 18; i++)
        h[i] = h[i-1] * (4 * i - 2) / (i + 1);
    memcpy(c, h, sizeof(h));
    for(int i = 2; i <= 18; i++) {
        h[i] += h[i-1];
        if(h[i] >= n) {
            m = i;
            break;
        }
    }
    dfs(m, n - h[m - 1]);
    return 0;
}

dfs(x,y) :x 点中排名为 y 的树

2024/10/2 23:20
加载中...