fork download
  1. import java.util.*;
  2. import java.lang.*;
  3. import java.io.*;
  4. import java.lang.Math;
  5.  
  6. class Main
  7. {
  8. public static int max = 5001;
  9. static int [] x = new int[max];
  10. public static void main (String[] args) throws java.lang.Exception
  11. {
  12. Scanner in = new Scanner(System.in);
  13. int n = in.nextInt();
  14. int k;
  15. x[1] = 1;
  16. x[2] = 2;
  17. for (int i = 3; i <= n; i++) {
  18. x[i] = x[i-1]+1;
  19. for (int j = 2; j <= Math.sqrt(n); j++) {
  20. if(i % j == 0) {
  21. k = x[j] + x[i/j];
  22. if( k < x[i]) x[i] = x[j] + x[i/j];
  23. }
  24. }
  25. }
  26. System.out.println(x[n]);
  27. }
  28. }
Success #stdin #stdout 0.07s 2184192KB
stdin
985
stdout
21