TLE求调
  • 板块灌水区
  • 楼主Tomwsc
  • 当前回复2
  • 已保存回复2
  • 发布时间2024/11/2 17:16
  • 上次更新2024/11/2 19:51:54
查看原帖
TLE求调
1418967
Tomwsc楼主2024/11/2 17:16

P1025

代码如下:

#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;
}
2024/11/2 17:16
加载中...