给你四个整数:n
、a
、b
、c
,请你设计一个算法来找出第 n
个丑数。
丑数是可以被 a
或 b
或 c
整除的 正整数 。
示例 1:
输入:n = 3, a = 2, b = 3, c = 5 输出:4 解释:丑数序列为 2, 3, 4, 5, 6, 8, 9, 10... 其中第 3 个是 4。
示例 2:
输入:n = 4, a = 2, b = 3, c = 4 输出:6 解释:丑数序列为 2, 3, 4, 6, 8, 9, 10, 12... 其中第 4 个是 6。
示例 3:
输入:n = 5, a = 2, b = 11, c = 13 输出:10 解释:丑数序列为 2, 4, 6, 8, 10, 11, 12, 13... 其中第 5 个是 10。
示例 4:
输入:n = 1000000000, a = 2, b = 217983653, c = 336916467 输出:1999999984
提示:
1 <= n, a, b, c <= 10^9
1 <= a * b * c <= 10^18
- 本题结果在
[1, 2 * 10^9]
的范围内
我们可以将题目转换为:找到最小的正整数
对于一个正整数
我们可以使用二分查找的方法找到最小的正整数
定义二分查找的左边界为
时间复杂度
class Solution:
def nthUglyNumber(self, n: int, a: int, b: int, c: int) -> int:
ab = lcm(a, b)
bc = lcm(b, c)
ac = lcm(a, c)
abc = lcm(a, b, c)
l, r = 1, 2 * 10**9
while l < r:
mid = (l + r) >> 1
if (
mid // a
+ mid // b
+ mid // c
- mid // ab
- mid // bc
- mid // ac
+ mid // abc
>= n
):
r = mid
else:
l = mid + 1
return l
class Solution {
public int nthUglyNumber(int n, int a, int b, int c) {
long ab = lcm(a, b);
long bc = lcm(b, c);
long ac = lcm(a, c);
long abc = lcm(ab, c);
long l = 1, r = 2000000000;
while (l < r) {
long mid = (l + r) >> 1;
if (mid / a + mid / b + mid / c - mid / ab - mid / bc - mid / ac + mid / abc >= n) {
r = mid;
} else {
l = mid + 1;
}
}
return (int) l;
}
private long gcd(long a, long b) {
return b == 0 ? a : gcd(b, a % b);
}
private long lcm(long a, long b) {
return a * b / gcd(a, b);
}
}
class Solution {
public:
int nthUglyNumber(int n, int a, int b, int c) {
long long ab = lcm(a, b);
long long bc = lcm(b, c);
long long ac = lcm(a, c);
long long abc = lcm(ab, c);
long long l = 1, r = 2000000000;
while (l < r) {
long long mid = (l + r) >> 1;
if (mid / a + mid / b + mid / c - mid / ab - mid / bc - mid / ac + mid / abc >= n) {
r = mid;
} else {
l = mid + 1;
}
}
return l;
}
long long lcm(long long a, long long b) {
return a * b / gcd(a, b);
}
long long gcd(long long a, long long b) {
return b == 0 ? a : gcd(b, a % b);
}
};
func nthUglyNumber(n int, a int, b int, c int) int {
ab, bc, ac := lcm(a, b), lcm(b, c), lcm(a, c)
abc := lcm(ab, c)
var l, r int = 1, 2e9
for l < r {
mid := (l + r) >> 1
if mid/a+mid/b+mid/c-mid/ab-mid/bc-mid/ac+mid/abc >= n {
r = mid
} else {
l = mid + 1
}
}
return l
}
func gcd(a, b int) int {
if b == 0 {
return a
}
return gcd(b, a%b)
}
func lcm(a, b int) int {
return a * b / gcd(a, b)
}
function nthUglyNumber(n: number, a: number, b: number, c: number): number {
const ab = lcm(BigInt(a), BigInt(b));
const bc = lcm(BigInt(b), BigInt(c));
const ac = lcm(BigInt(a), BigInt(c));
const abc = lcm(BigInt(a), bc);
let l = 1n;
let r = BigInt(2e9);
while (l < r) {
const mid = (l + r) >> 1n;
const count =
mid / BigInt(a) +
mid / BigInt(b) +
mid / BigInt(c) -
mid / ab -
mid / bc -
mid / ac +
mid / abc;
if (count >= BigInt(n)) {
r = mid;
} else {
l = mid + 1n;
}
}
return Number(l);
}
function gcd(a: bigint, b: bigint): bigint {
return b === 0n ? a : gcd(b, a % b);
}
function lcm(a: bigint, b: bigint): bigint {
return (a * b) / gcd(a, b);
}