Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- public int findKthNumber(int m, int n, int k) {
- int lo = 1;
- int hi = m * n;
- while (lo < hi - 1) {
- int mid = lo + (hi - lo) / 2;
- if (d(mid) < k) lo = mid;
- else hi = mid;
- }
- System.out.println(lo);
- int s = d(lo);
- int x = lo + 1;
- while (s < k) s += theta(x++);
- return x -1;
- }
- private int d(int x) {
- int u = 1;
- while (u*u <= x) u++;
- u--;
- int s = 0;
- for (int k = 1; k <= u; k++) s += 2 * (x / k);
- s -= u*u;
- return s;
- }
- private int theta(int x) {
- if (x == 1) return 1;
- int u = 1;
- while (u*u <= x) u++;
- u--;
- int num = 0;
- for (int k = 1; k <= u; k++)
- if (x % k == 0) num += 2;
- if (x == u*u) num--;
- return num;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement