fork download
  1.  
  2. import java.io.*;
  3. import java.util.*;
  4.  
  5. public class EvenNumberOfDivisors {
  6.  
  7. static PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));
  8. static StringTokenizer st = new StringTokenizer("");
  9.  
  10. public static String next() {
  11. try {
  12. while (!st.hasMoreTokens()) {
  13. String s = br.readLine();
  14. if (s == null)
  15. return null;
  16. st = new StringTokenizer(s);
  17. }
  18. return st.nextToken();
  19. } catch(Exception e) {
  20. return null;
  21. }
  22. }
  23. public static void main(String[] asda) throws Exception {
  24. long max = 10000000000000000L + 5;
  25. long [] squares = new long [ 1 + (int)(Math.sqrt(max)) ];
  26. for (long k = 1; k*k <= max; k++) {
  27. squares[(int)(k-1)] = k*k;
  28. }
  29.  
  30. while (true) {
  31. long N = Long.parseLong( next() );
  32. long M = Long.parseLong( next() );
  33.  
  34. if (N == 0 && M == 0) {
  35. break;
  36. }
  37.  
  38. long size = M - N + 1;
  39. long x = f(squares, N, M);
  40. long ans = size - x;
  41. out.println(ans);
  42. }
  43. //
  44. out.flush();
  45. System.exit(0);
  46. }
  47.  
  48. // nuumber of values in range [L, R] where the value has odd number of divisor (perfect square)
  49. static int f(long [] squares, long L, long R) {
  50. int low = firstHigheer(squares, L);
  51. int high = firstLower(squares, R);
  52. // out.printf("f(%d, %d) = %d - %d\n", L, R, low, high );
  53. return high - low + 1;
  54. }
  55.  
  56.  
  57. // the first index with value >= key
  58. static int firstHigheer(long [] list, long key) {
  59. int lo = 0, hi = list.length - 1;
  60. int ans = -1;
  61. while ( lo <= hi ) {
  62. int mid = (lo + hi) >> 1;
  63. if ( list[mid] >= key ) {
  64. ans = mid; hi = mid - 1;
  65. } else lo = mid + 1;
  66. }
  67. return ans;
  68. }
  69.  
  70. // the last index with value <= key
  71. static int firstLower(long [] list, long key) {
  72. int lo = 0, hi = list.length - 1;
  73. int ans = -1;
  74. while ( lo <= hi ) {
  75. int mid = (lo + hi) >> 1;
  76. if ( list[mid] <= key ) {
  77. ans = mid; lo = mid + 1;
  78. } else hi = mid - 1;
  79. }
  80. return ans;
  81. }
  82.  
  83. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
Main.java:5: error: class EvenNumberOfDivisors is public, should be declared in a file named EvenNumberOfDivisors.java
public class EvenNumberOfDivisors {
       ^
1 error
stdout
Standard output is empty