Talentd
FleetCode

878. Nth Magical Number

Hard37.1% acceptance3 companies

A positive integer is magical if it is divisible by either a or b.

Given the three integers n, a, and b, return the nth magical number. Since 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

Constraints:

  • 1 <= n <= 109
  • 2 <= a, b <= 4 * 104

Asked at 3 companies

23:10
Still stuck?Read 3 approaches, or watch a walkthrough.
Open solution
Loading editor…
1class Solution:
2 def nthMagicalNumber(self, n: int, a: int, b: int) -> int:
3
Starter codePython 3Spaces: 4
Examples
Input
1
2
3
Input
4
2
3
Your code is saved on this device.Solution