Ugly numbers are positive integers which are divisible by aorborc.
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 3rd is 4.
Nothing special. Still finding the Kth-Smallest. We need to design an enough function, given an input num, determine whether there are at least n ugly numbers less than or equal to num. Since a might be a multiple of b or c, or the other way round, we need the help of greatest common divisor to avoid counting duplicate numbers.
defnthUglyNumber(n:int,a:int,b:int,c:int) ->int:defenough(num) ->bool: total = mid//a + mid//b + mid//c - mid//ab - mid//ac - mid//bc + mid//abcreturn total >= n ab = a * b // math.gcd(a, b) ac = a * c // math.gcd(a, c) bc = b * c // math.gcd(b, c) abc = a * bc // math.gcd(a, bc) left, right =1,10**10while left < right: mid = left + (right - left) //2ifenough(mid): right = midelse: left = mid +1return left