Skip to content

Practice Nth Magical Number

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Hard#878
LeetCode Problem

Nth Magical Number

Problem Statement

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 10^9 + 7.

Source: Nth Magical Number on LeetCode. Use the original task for its coding environment and submissions.

Examples

# 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 <= 10^9
2 <= a, b <= 4 * 10^4

Related LeetCode Problems

Practice more problems similar to Nth Magical Number.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.