Advertisement
ogv

Untitled

ogv
Oct 1st, 2019
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.03 KB | None | 0 0
  1. class Solution {
  2. public int findKthNumber(int m, int n, int k) {
  3. int lo = 1;
  4. int hi = m * n;
  5.  
  6. while (lo < hi - 1) {
  7. int mid = lo + (hi - lo) / 2;
  8. if (d(mid) < k) lo = mid;
  9. else hi = mid;
  10. }
  11.  
  12. System.out.println(lo);
  13.  
  14. int s = d(lo);
  15. int x = lo + 1;
  16. while (s < k) s += theta(x++);
  17.  
  18. return x -1;
  19. }
  20.  
  21. private int d(int x) {
  22. int u = 1;
  23. while (u*u <= x) u++;
  24. u--;
  25.  
  26. int s = 0;
  27. for (int k = 1; k <= u; k++) s += 2 * (x / k);
  28. s -= u*u;
  29.  
  30. return s;
  31. }
  32.  
  33. private int theta(int x) {
  34. if (x == 1) return 1;
  35.  
  36. int u = 1;
  37. while (u*u <= x) u++;
  38. u--;
  39.  
  40. int num = 0;
  41. for (int k = 1; k <= u; k++)
  42. if (x % k == 0) num += 2;
  43.  
  44. if (x == u*u) num--;
  45.  
  46. return num;
  47. }
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement