社区讨论

TLE求调

灌水区参与者 2已保存回复 2

讨论操作

快速查看讨论及其快照的属性,并进行相关操作。

当前回复
2 条
当前快照
1 份
快照标识符
@m2zyaart
此快照首次捕获于
2024/11/02 17:17
去年
此快照最后确认于
2025/11/04 15:31
4 个月前
查看原帖
代码如下:
CPP
#include<iostream>
#include<cmath>
#include<algorithm>
#define int long long
using namespace std;
int n , k;
int a[10] = {1};
int cnt , ans;
 
inline void print() {
    for(int i = 1;i <= k;i ++)
        cout << a[i] << " ";
    cout << '\n';
    return;
}
 
void dfs(int x) {
//  cout << ans << " " << x << '\n';
    if(ans > n)
        return;
    if(x == k + 1)  {
        if(ans == n) {
            cnt ++;
//          print();
        }
        return;
    }
    for(int i = a[x - 1];i <= (n - ans) / (k - x + 1);i ++) {
//      cout << ans << " " << x << " " << n - ans << " " << k - x - 1 << " " << (n - ans) / (n - x) << '\n';
        a[x] = i;
        ans += i;
        dfs(x + 1);
        ans -= i;
    }
    return;
}
 
void dfs1(int x) {
//  cout << x << '\n';
    if(ans > n)
        return;
    if(x == k + 1) {
        if(ans == n) 
            print();
        return;
    }
    for(int i = a[x - 1];i <= n;i ++) {
        a[x] = i;
        ans += i;
        dfs1(x + 1);
        ans -= i;
    }
    return;
}
 
signed main() {
    ios::sync_with_stdio(0) , cin.tie(0) , cout.tie(0);
    cin >> n >> k;
//  cout << (n - 2) / (k - 3 + 1) << '\n';
    dfs(1);
//  cout << '\n';
    ans = 0;
//  dfs1(1);
    cout << cnt;
    return 0;
}

回复

2 条回复,欢迎继续交流。

正在加载回复...