fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static boolean isPrime(int num) {
  11. if (num < 2)
  12. return false;
  13.  
  14. for (int i = 2; i * i <= num; i++) {
  15. if (num % i == 0)
  16. return false;
  17. }
  18.  
  19. return true;
  20. }
  21.  
  22. public static void main (String[] args) throws java.lang.Exception
  23. {
  24. // your code goes here
  25. String s = "11375";
  26.  
  27. int N = s.length();
  28.  
  29. int[] dp = new int[N];
  30.  
  31. if(isPrime(Integer.parseInt(s.substring(0,1)))){
  32. dp[0] = 1;
  33. }
  34.  
  35.  
  36. for(int i = 1; i < N; i++){
  37.  
  38. for(int j = i; j >= Math.max(0, i - 6); j--){
  39. if (s.charAt(j) == '0') continue;
  40. int num = Integer.parseInt(s.substring(j, i + 1));
  41. if(isPrime(num)){
  42. if(j == 0){
  43. dp[i] += 1;
  44. }else{
  45. dp[i] += dp[j-1];
  46. }
  47. }
  48. }
  49. }
  50.  
  51. System.out.println(dp[N-1]);
  52. }
  53. }
Success #stdin #stdout 0.07s 52476KB
stdin
Standard input is empty
stdout
3