forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ugly-number-iii.cpp
42 lines (38 loc) · 1.16 KB
/
ugly-number-iii.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
// Time: O(logn)
// Space: O(1)
class Solution {
public:
int nthUglyNumber(int n, int a, int b, int c) {
int64_t lcm_a_b = lcm(a, b), lcm_b_c = lcm(b, c), lcm_c_a = lcm(c, a);
int64_t lcm_a_b_c = lcm(lcm_a_b, lcm_b_c);
int64_t left = 1, right = 2 * 1e9;
while (left <= right) {
const auto& mid = left + (right - left) / 2;
if (count(mid, a, b, c,
lcm_a_b, lcm_b_c, lcm_c_a, lcm_a_b_c) >= n) {
right = mid - 1;
} else {
left = mid + 1;
}
}
return left;
}
private:
int64_t count (int64_t x,
int64_t a, int64_t b, int64_t c,
int64_t lcm_a_b, int64_t lcm_b_c, int64_t lcm_c_a,
int64_t lcm_a_b_c) {
return x / a + x / b + x / c - (x / lcm_a_b + x / lcm_b_c + x / lcm_c_a) + x / lcm_a_b_c;
}
int64_t gcd(int64_t a, int64_t b) {
while (b != 0) {
const auto tmp = b;
b = a % b;
a = tmp;
}
return a;
}
int64_t lcm(int64_t a, int64_t b) {
return a / gcd(a, b) * b;
}
};