Math Fundamentals18 sections · 814 units
Open in Course

Problem - Nth Magical Number

LeetCode 878

A number is magical if it is divisible by a or by b. Find the nth magical number modulo 10^9 + 7.

Example: a = 2, b = 3, n = 4. Magical numbers are 2, 3, 4, 6, 8, 9, 10, 12... The 4th is 6.

Constraints: 1 ≤ n ≤ 10^9, 2 ≤ a, b ≤ 40000. You cannot list all magical numbers. You need a formula.