Advertisement
ogv

Untitled

ogv
Oct 1st, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.07 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, int m, int n) {
  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++) {
  28. if
  29. s += 2 * (x / k);
  30. }
  31. s -= u*u;
  32.  
  33. return s;
  34. }
  35.  
  36. private int theta(int x) {
  37. if (x == 1) return 1;
  38.  
  39. int u = 1;
  40. while (u*u <= x) u++;
  41. u--;
  42.  
  43. int num = 0;
  44. for (int k = 1; k <= u; k++)
  45. if (x % k == 0) num += 2;
  46.  
  47. if (x == u*u) num--;
  48.  
  49. return num;
  50. }
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement