fork download
  1. #include <iostream>
  2. #include <algorithm>
  3.  
  4. using namespace std;
  5. int dp[1000001] = { 0, };
  6. int main(void)
  7. {
  8. int n;
  9. cin >> n;
  10.  
  11. for (int i = 2; i <= n; i++)
  12. {
  13. dp[i] = dp[i - 1] + 1;
  14. if (i % 2 == 0) dp[i] = min(dp[i], dp[i / 2] + 1);
  15. if (i % 3 == 0) dp[i] = min(dp[i], dp[i / 3] + 1);
  16. }
  17. cout << dp[n];
  18. }
Success #stdin #stdout 0s 4928KB
stdin
10
stdout
3