forked from shuboc/LeetCode-2
-
Notifications
You must be signed in to change notification settings - Fork 1
/
nth-magical-number.py
55 lines (51 loc) · 1.14 KB
/
nth-magical-number.py
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
43
44
45
46
47
48
49
50
51
52
53
54
55
# Time: O(logn)
# Space: O(1)
# A positive integer is magical if it is divisible by either A or B.
#
# Return the N-th magical number.
# Since the answer may be very large, return it modulo 10^9 + 7.
#
# Example 1:
#
# Input: N = 1, A = 2, B = 3
# Output: 2
# Example 2:
#
# Input: N = 4, A = 2, B = 3
# Output: 6
# Example 3:
#
# Input: N = 5, A = 2, B = 4
# Output: 10
# Example 4:
#
# Input: N = 3, A = 6, B = 4
# Output: 8
#
# Note:
# - 1 <= N <= 10^9
# - 2 <= A <= 40000
# - 2 <= B <= 40000
class Solution(object):
def nthMagicalNumber(self, N, A, B):
"""
:type N: int
:type A: int
:type B: int
:rtype: int
"""
def gcd(a, b):
while b:
a, b = b, a % b
return a
def check(A, B, N, lcm, target):
return target//A + target//B - target//lcm >= N
lcm = A*B // gcd(A, B)
left, right = min(A, B), max(A, B)*N
while left <= right:
mid = left + (right-left)//2
if check(A, B, N, lcm, mid):
right = mid-1
else:
left = mid+1
return left % (10**9 + 7)