ProgMe

B

Jun 15th, 2023
1,199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.21 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <vector>
  4. #include <numeric>
  5.  
  6. #define int int64_t
  7.  
  8. const int kN = 10001;
  9.  
  10. int f[kN][kN];
  11.  
  12. int calc(int n, int m) {
  13.     if (m == 0) {
  14.         if (n) {
  15.             return 0;
  16.         } else {
  17.             return 1;
  18.         }
  19.     }
  20.     if (!f[n][m]) {
  21.         for (int i = 0; i <= n; i++) {
  22.             f[n][m] += calc(n - i, m - 1);
  23.             if (f[n][m] > std::numeric_limits<int32_t>::max()) {
  24.                 f[n][m] = std::numeric_limits<int32_t>::max();
  25.                 break;
  26.             }
  27.         }
  28.     }
  29.     return f[n][m];
  30. }
  31.  
  32. void calculate(int n, int m, int k, std::vector<int> &v) {
  33.     if (m == 0) {
  34.         for (auto i : v) {
  35.             std::cout << i << ' ';
  36.         }
  37.         return;
  38.     }
  39.     for (int i = n; i >= 0; i--) {
  40.         int cnt = calc(n - i, m - 1);
  41.         if (k >= cnt) {
  42.             k -= cnt;
  43.         } else {
  44.             v.push_back(i);
  45.             calculate(n - i, m - 1, k, v);
  46.             return;
  47.         }
  48.     }
  49. }
  50.  
  51. int32_t main() {
  52.     std::ios::sync_with_stdio(false);
  53.     std::cin.tie(nullptr);
  54.     int n, m, k;
  55.     std::cin >> n >> m >> k;
  56.     std::vector<int> v;
  57.     calculate(n, m, k - 1, v);
  58. }
  59.  
Advertisement
Add Comment
Please, Sign In to add comment