1201. Ugly Number III
Difficulty: Medium
Topics: Senior, Math, Binary Search, Combinatorics, Number Theory, Least Common Multiple, Euclidean Algorithm, Greatest Common Divisor, Inclusion-Exclusion Principle, Weekly Contest 155
An ugly number is a positive integer that is divisible by a, b, or c.
Given four integers n, a, b, and c, return the nᵗʰ ugly number.
Example 1:
- Input: n = 3, a = 2, b = 3, c = 5
- Output: 4
- Explanation: The ugly numbers are 2, 3, 4, 5, 6, 8, 9, 10... The 3ʳᵈ is 4.
Example 2:
- Input: n = 4, a = 2, b = 3, c = 4
- Output: 6
- Explanation: The ugly numbers are 2, 3, 4, 6, 8, 9, 10, 12... The 4ᵗʰ is 6.
Example 3:
- Input: n = 5, a = 2, b = 11, c = 13
- Output: 10
- Explanation: The ugly numbers are 2, 4, 6, 8, 10, 11, 12, 13... The 5ᵗʰ is 10.
Example 4:
- Input: n = 1, a = 2, b = 3, c = 5
- Output: 2
- Explanation: The ugly numbers are 2, 3, 4, 5, 6, 8, 9, 10... The 1ˢᵗ is 2.
Example 5:
- Input: n = 6, a = 2, b = 3, c = 5
- Output: 8
- Explanation: The ugly numbers are 2, 3, 4, 5, 6, 8, 9, 10... The 6ᵗʰ is 8.
Example 6:
- Input: n = 7, a = 2, b = 3, c = 5
- Output: 9
- Explanation: The ugly numbers are 2, 3, 4, 5, 6, 8, 9, 10... The 7ᵗʰ is 9.
Example 7:
- Input: n = 10, a = 2, b = 3, c = 5
- Output: 14
- Explanation: The ugly numbers are 2, 3, 4, 5, 6, 8, 9, 10, 12, 14... The 10ᵗʰ is 14.
Example 8:
- Input: n = 5, a = 2, b = 2, c = 2
- Output: 10
- Explanation: The ugly numbers are 2, 4, 6, 8, 10, 12... The 5ᵗʰ is 10.
Example 9:
- Input: n = 1, a = 1, b = 1, c = 1
- Output: 1
- Explanation: Since every positive integer is divisible by 1, the ugly numbers are 1, 2, 3, 4, 5... The 1ˢᵗ is 1.
Example 10:
- Input: n = 1000000000, a = 1, b = 1, c = 1
- Output: 1000000000
- Explanation: Since every positive integer is divisible by 1, every positive integer is ugly. Therefore, the nᵗʰ ugly number is n, which is 1000000000.
Constraints:
1 <= n, a, b, c <= 10⁹1 <= a * b * c <= 10¹⁸- It is guaranteed that the result will be in range
[1, 2 * 10⁹].
Hint:
- Write a function f(k) to determine how many ugly numbers smaller than k. As f(k) is non-decreasing, try binary search.
- Find all ugly numbers in 1, LCM(a, b, c). Use inclusion-exclusion principle to expand the result.
Similar Questions:
Solution:
We solve the problem by binary-searching the answer and counting how many ugly numbers are <= mid using the inclusion-exclusion principle. The count function is monotonic, so the smallest value where count >= n is the nᵗʰ ugly number.
Approach
- Precompute pairwise and triple LCMs:
-
lcm(a,b),lcm(a,c),lcm(b,c),lcm(a,b,c)
-
- Use the Euclidean algorithm to compute GCD.
- Use
lcm(x,y) = x / gcd(x,y) * yto avoid overflow where possible. - Define the count of ugly numbers
<= x:- Add
⌊x/a⌋ + ⌊x/b⌋ + ⌊x/c⌋ - Subtract
⌊x/lcm(a,b)⌋ + ⌊x/lcm(a,c)⌋ + ⌊x/lcm(b,c)⌋ - Add
⌊x/lcm(a,b,c)⌋
- Add
- Binary-search the smallest
xsuch thatcount(x) >= n. - Return that
x.
Let's implement this solution in PHP: 1201. Ugly Number III
<?php
/**
* @param Integer $n
* @param Integer $a
* @param Integer $b
* @param Integer $c
* @return Integer
*/
function nthUglyNumber(int $n, int $a, int $b, int $c): int
{
...
...
...
/**
* go to ./solution.php
*/
}
// Test cases
echo nthUglyNumber(3, 2, 3, 5) . "\n"; // Output: 4
echo nthUglyNumber(4, 2, 3, 4) . "\n"; // Output: 6
echo nthUglyNumber(5, 2, 11, 13) . "\n"; // Output: 10
echo nthUglyNumber(1, 2, 3, 5) . "\n"; // Output: 2
echo nthUglyNumber(6, 2, 3, 5) . "\n"; // Output: 8
echo nthUglyNumber(7, 2, 3, 5) . "\n"; // Output: 9
echo nthUglyNumber(10, 2, 3, 5) . "\n"; // Output: 14
echo nthUglyNumber(5, 2, 2, 2) . "\n"; // Output: 10
echo nthUglyNumber(1, 1, 1, 1) . "\n"; // Output: 1
echo nthUglyNumber(1000000000, 1, 1, 1) . "\n"; // Output: 1000000000
?>
Explanation
- An ugly number is any positive integer divisible by
a,b, orc. - The count function
count(x)counts how many integers in[1, x]are divisible by at least one ofa,b, orc. - Inclusion-exclusion avoids double-counting numbers divisible by multiple values.
-
count(x)is non-decreasing:- As
xincreases, the number of ugly numbers up toxcan only increase or stay the same.
- As
- Therefore, binary search can find the smallest
xwhere at leastnugly numbers exist. - That smallest
xmust itself be thenᵗʰugly number. - The upper bound
2_000_000_000is safe because the problem guarantees the result is within[1, 2 * 10⁹].
Complexity Analysis
- Precomputing LCMs:
-
O(log(max(a,b,c)))time for GCD computations
-
- Binary search:
-
O(log(2 * 10⁹)), which is about31iterations
-
- Each binary-search step:
-
O(1)arithmetic and integer-division operations
-
- Overall time complexity:
-
O(log R + log M), whereR = 2 * 10⁹andM = max(a,b,c) - Effectively
O(log R)
-
- Space complexity:
O(1)
Contact Links
If you found this series helpful, please consider giving the repository a star on GitHub or sharing the post on your favorite social networks 😍. Your support would mean a lot to me!

If you want more helpful content like this, feel free to follow me:
Top comments (0)