Nth Magical Number

Given three integers n, a, and b, return the n-th positive integer that is divisible by either a or b. Return the answer modulo 1e9+7.

Input Format

n, a, b = integers

Output Format

n-th magical number modulo 1e9+7

Constraints

  • 1 <= n <= 10^9; 2 <= a, b <= 4 * 10^4

Examples

Example 1:

Input:

n = 1
a = 2
b = 3

Output:

2

Explanation:

The first magical number divisible by 2 or 3 is 2.

Example 2:

Input:

n = 4
a = 2
b = 3

Output:

6

Explanation:

The magical numbers are 2, 3, 4, 6; the 4th is 6.

Loading...
Nth Magical Number - Math DSA Problem