Advertisement
LEGEND2004

G - Decreasing Number

Aug 31st, 2023
746
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.43 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define int long long
  5. const int N = 1e6 + 5;
  6. int n;
  7. int dp[N];
  8.  
  9.  
  10. signed main()
  11. {
  12.     dp[1] = 0;
  13.     for(int i = 2; i <= 1e6; i++){
  14.         dp[i] = dp[i - 1] + 1;
  15.         if(i % 2 == 0)
  16.             dp[i] = min(dp[i] , dp[i / 2] + 1);
  17.         if(i % 3 == 0)
  18.             dp[i] = min(dp[i] , dp[i / 3] + 1);
  19.     }
  20.     while(cin >> n){
  21.         cout << dp[n] << endl;
  22.     }
  23. }
  24.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement