#878

Nth Magical Number

master · 1810 · lc hard +32 · verified · 36.5% accepted · 1,336 likes · top 14%

Description

A positive integer is magical if it is divisible by a or by b (or both).

Given integers n, a, and b, return the nth magical number. Because the answer may be very large, return it modulo 109 + 7.

Example 1:

Input: n = 1, a = 2, b = 3
Output: 2

Example 2:

Input: n = 4, a = 2, b = 3
Output: 6

Code

1
2
3