为什么只有这1个测试点TLE,大佬给看看,在线等!!!
查看原帖
为什么只有这1个测试点TLE,大佬给看看,在线等!!!
675208
coder2009楼主2023/7/6 21:39

1个测试点TLE,求给看看!

#include <bits/stdc++.h>

using namespace std;
#define endl '\n';
int v[20];
bool b[20];
int Pas_tri[20][20];
int p, n;
bool is = true;

void dfs(int t, int s) {
    if (s > p) {
        return;
    }
    if (t > n) {
        if (s == p && is) {
            for (int i = 1; i <= n; ++i) {
                cout << v[i] << " " ;
            }
            is = false;
        }
        return;
    }
    for (int i = 1; i <= n; ++i) {
        if (!b[i]) {
            b[i] = true;
            v[t] = i;
            dfs(t + 1, s + i * Pas_tri[n][t]);
            b[i] = false;
        }
    }
}

void best_coder() {
    cin >> n >> p;
    Pas_tri[1][1] = 1;
    for (int i = 2; i <= n; ++i){
        for (int j = 1; j <= i; ++j) {
            Pas_tri[i][j] = Pas_tri[i - 1][j] + Pas_tri[i - 1][j - 1];
        }
    }

    dfs(1,0);
}

void happy_coder() {
}

int main() {
    // 提升cin、cout效率
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);

    // 小码匠
    best_coder();

    // 最优解
    // happy_coder();

    // 返回
    return 0;
}
2023/7/6 21:39
加载中...