fork download
  1. import java.util.*;
  2. import java.lang.*;
  3. import java.io.*;
  4.  
  5. public class Main
  6. {
  7. int[] x = new int[1000001];
  8.  
  9. public int operations(int n)
  10. {
  11. x[1] = 0;
  12. for (int i = 2; i <= n; i++)
  13. {
  14. x[i] = x[i-1] + 1;
  15. if (i%2 == 0)
  16. {
  17. x[i] = Math.min(x[i], x[i/2] + 1);
  18. }
  19. if (i%3 == 0)
  20. {
  21. x[i] = Math.min(x[i], x[i/3] + 1);
  22. }
  23. }
  24. return x[n];
  25. }
  26.  
  27.  
  28. public static void main (String[] args)
  29. {
  30. Scanner in = new Scanner(System.in);
  31. Main x = new Main();
  32. while (in.hasNextInt())
  33. {
  34. int n = in.nextInt();
  35. System.out.println(x.operations(n));
  36. }
  37. }
  38. }
Success #stdin #stdout 0.14s 33532KB
stdin
256
1037
8771
9022
102651
stdout
8
11
13
13
19