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:
    def nthMagicalNumber(self, N: int, A: int, B: int) -> int:
        a_t = A
        b_t = B
        col = []
        low_anc = a_t
        while a_t != b_t:
            if a_t < b_t:
                col.append(a_t)
                a_t = (a_t + A) #% (10 ** 9 + 7)
            else:
                col.append(b_t)
                b_t = (b_t + B) #% (10 ** 9 + 7)
        col.append(a_t)
        low_anc = a_t
        
        
        idx = (N - 1) % len(col)
        
        # 2,3,4,6
        
        ans = (N - 1) // len(col) * low_anc + col[idx]
        
        return ans % (10 ** 9 + 7)
class Solution:
    def nthMagicalNumber(self, N: int, A: int, B: int) -> int:
        a_t = A
        b_t = B
        col = []
        low_anc = a_t
        while a_t != b_t:
            if a_t < b_t:
                col.append(a_t)
                a_t = (a_t + A) #% (10 ** 9 + 7)
            else:
                col.append(b_t)
                b_t = (b_t + B) #% (10 ** 9 + 7)
        col.append(a_t)
        low_anc = a_t
        
        
        idx = (N - 1) % len(col)
        
        # 2,3,4,6
        
        ans = (N - 1) // len(col) * low_anc + col[idx]
        
        return ans % (10 ** 9 + 7)