Class Solution

java.lang.Object
g0801_0900.s0878_nth_magical_number.Solution

public class Solution extends Object
878 - Nth Magical Number.<p>Hard</p> <p>A positive integer is <em>magical</em> if it is divisible by either <code>a</code> or <code>b</code>.</p> <p>Given the three integers <code>n</code>, <code>a</code>, and <code>b</code>, return the <code>n<sup>th</sup></code> magical number. Since the answer may be very large, <strong>return it modulo</strong> <code>10<sup>9</sup> + 7</code>.</p> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> n = 1, a = 2, b = 3</p> <p><strong>Output:</strong> 2</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> n = 4, a = 2, b = 3</p> <p><strong>Output:</strong> 6</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 <= n <= 10<sup>9</sup></code></li> <li><code>2 <= a, b <= 4 * 10<sup>4</sup></code></li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • nthMagicalNumber

      public int nthMagicalNumber(int n, int a, int b)