Advertisement
ssrtatarin

Untitled

Jul 8th, 2025
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.18 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. #include <ext/pb_ds/assoc_container.hpp>
  3. using namespace __gnu_pbds;
  4. using namespace std;
  5. #define int long long
  6. using ll = long long;
  7. using ld = long double;
  8. const int N = 1000;
  9. const int N2 = 3e5 * 33;
  10. const int C = 10000;
  11. const ld inf_ld = 1e17;
  12. const int inf = 1e9;
  13.  
  14.  
  15.  
  16. void solve() {
  17.     int n;
  18.     cin >> n;
  19.     vector<int> dp (n + 1, inf);
  20.     vector<int> pr(n + 1, -1);
  21.     dp[1] = 0;
  22.     for (int i = 2; i <= n; ++i) {
  23.         dp[i] = dp[i - 1] + 1;
  24.         pr[i] = i - 1;
  25.         if (i % 2 == 0 && dp[i] > dp[i / 2] + 1) {
  26.             dp[i] = min(dp[i], dp[i / 2] + 1);
  27.             pr[i] = i / 2;
  28.         }
  29.         if (i % 3 == 0 && dp[i] > dp[i / 3] + 1) {
  30.             dp[i] = min(dp[i], dp[i / 3] + 1);
  31.             pr[i] = i / 3;
  32.         }
  33.  
  34.     }
  35.     cout << dp[n] << '\n';
  36.     vector<int> res;
  37.     while (n != -1) {
  38.         res.push_back(n);
  39.         n = pr[n];
  40.     }
  41.     reverse(res.begin(), res.end());
  42.     for (auto &x : res) cout << x << ' ';
  43.  
  44.  
  45. }
  46.  
  47.  
  48.  
  49.  
  50. signed main(){
  51.  
  52.     ios_base ::sync_with_stdio(false);
  53.     cin.tie(nullptr);
  54.     int test = 1;
  55.     //cin >> test;
  56.     while (test--) {
  57.         solve();
  58.     }
  59. }
  60.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement