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

Return the N-th magical number.  Since the answer may be very large, return it modulo 10^9 + 7.

Example 1:

Input: N = 1, A = 2, B = 3
Output: 2

Example 2:

Input: N = 4, A = 2, B = 3
Output: 6

Example 3:

Input: N = 5, A = 2, B = 4
Output: 10

Example 4:

Input: N = 3, A = 6, B = 4
Output: 8

Note:

  1. 1 <= N <= 10^9
  2. 2 <= A <= 40000
  3. 2 <= B <= 40000

Approach #1: Binary Serach + Brute Froce. [Time limit exceeded]

class Solution {
public:
int nthMagicalNumber(int N, int A, int B) {
long l = 1, r = N * min(A, B);
while (l <= r) {
long m = l + (r - l) / 2;
int num = 0;
for (int i = 1; i <= m; ++i) {
if (i % A == 0 || i % B == 0)
num++;
}
if (num >= N) r = m - 1;
else l = m + 1;
}
return l;
}
};

  

Approach #2: Binary Serach + LCM.

class Solution {
public:
int nthMagicalNumber(int N, int A, int B) {
int MOD = 1e9 + 7;
int L = A / gcd(A, B) * B; long l = 0, r = (long) 1e15;
while (l < r) {
long m = l + (r - l) / 2;
long num = m / A + m / B - m / L; // not int type
if (num < N) l = m + 1;
else r = m;
}
return (int) (l % MOD);
}
private:
int gcd(int x, int y) {
if (x == 0) return y;
return gcd(y % x, x);
}
};
Runtime: 0 ms, faster than 100.00% of C++ online submissions for Nth Magical Number.

 Approach #3: Mathemacital.
class Solution {
public:
int nthMagicalNumber(int N, int A, int B) {
int MOD = 1e9 + 7;
int L = A / gcd(A, B) * B;
int M = L / A + L / B - 1;
int q = N / M, r = N % M; long ans = (long) q * L % MOD;
if (r == 0)
return (int) ans; int heads[2] = {A, B};
for (int i = 0; i < r - 1; ++i) {
if (heads[0] <= heads[1])
heads[0] += A;
else
heads[1] += B;
} ans += min(heads[0], heads[1]);
return (int) (ans % MOD);
} int gcd(int x, int y) {
if (x == 0) return y;
return gcd(y % x, x);
}
};

  

878. Nth Magical Number的更多相关文章

  1. [LeetCode] 878. Nth Magical Number 第N个神奇数字

    A positive integer is magical if it is divisible by either A or B. Return the N-th magical number.  ...

  2. 【leetcode】878. Nth Magical Number

    题目如下: 解题思路:本题求的是第N个Magical Number,我们可以很轻松的知道这个数的取值范围 [min(A,B), N*max(A,B)].对于知道上下界求具体数字的题目,可以考虑用二分查 ...

  3. [Swift]LeetCode878. 第 N 个神奇数字 | Nth Magical Number

    A positive integer is magical if it is divisible by either A or B. Return the N-th magical number.  ...

  4. (斐波那契总结)Write a method to generate the nth Fibonacci number (CC150 8.1)

    根据CC150的解决方式和Introduction to Java programming总结: 使用了两种方式,递归和迭代 CC150提供的代码比较简洁,不过某些细节需要分析. 现在直接运行代码,输 ...

  5. 【Leetcode_easy】1137. N-th Tribonacci Number

    problem 1137. N-th Tribonacci Number solution: class Solution { public: int tribonacci(int n) { ) ; ...

  6. [LeetCode] 1137. N-th Tribonacci Number

    Description e Tribonacci sequence Tn is defined as follows: T0 = 0, T1 = 1, T2 = 1, and Tn+3 = Tn + ...

  7. LeetCode.1137-第N个泰波那契数(N-th Tribonacci Number)

    这是小川的第409次更新,第441篇原创 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第260题(顺位题号是1137).Tribonacci(泰波那契)序列Tn定义如下: 对于n&g ...

  8. 1137. N-th Tribonacci Number(Memory Usage: 13.9 MB, less than 100.00% of Python3)

    其实思路很简单,套用一下普通斐波那契数列的非递归做法即可,不过这个成绩我一定要纪念一下,哈哈哈哈哈 代码在这儿: class Solution: def tribonacci(self, n: int ...

  9. 【leetcode】1137. N-th Tribonacci Number

    题目如下: The Tribonacci sequence Tn is defined as follows: T0 = 0, T1 = 1, T2 = 1, and Tn+3 = Tn + Tn+1 ...

随机推荐

  1. [WASM] Run WebAssembly in Node.js using the node-loader

    WebAssembly is great for targeting performance bottlenecks in the browser. Now with node-loader, we ...

  2. android studio——Could not find method externalNativeBuild()

    gradle同步工程时出现错误 Error:(36, 0) Could not find method externalNativeBuild() for arguments [build_cazi7 ...

  3. centos Linux 常用命令汇总

    CentOS 关闭防火墙 1) 永久性生效,重启后不会复原 开启: chkconfig iptables on 关闭: chkconfig iptables off 2) 即时生效,重启后复原 开启: ...

  4. Qt、C++ 简易计算器

    Qt.C++实现简易计算器: 以下内容是我实现这个简易计算器整个过程,其中包括我对如何实现这个功能的思考.中途遇到的问题.走过的弯路 整个实现从易到难,计算器功能从简单到复杂,最开始设计的整个实现步骤 ...

  5. 解决:Android4.3锁屏界面Emergency calls only - China Unicom与EMERGENCY CALL语义反复

    从图片中我们能够看到,这里在语义上有一定的反复,当然这是谷歌的原始设计.这个问题在博客上进行共享从表面上来看着实没有什么太大的意义,只是因为Android4.3在锁屏功能上比起老版本号做了非常大的修改 ...

  6. ES6常用语法简介import export

    ES6常用语法简介import export let与var用法区别 //var var a = []; for (var i = 0; i < 10; i++) { a[i] = functi ...

  7. 白帽子讲web安全读后感

    又是厚厚的一本书,为了不弄虚做假,只得变更计划,这一次调整为读前三章,安全世界观,浏览器安全和xss.其它待用到时再专门深入学习. 吴翰清是本书作者,icon是一个刺字,圈内人称道哥.曾供职于阿里,后 ...

  8. MongoDB Spark Connector

    [在文件/usr/bin/yum./usr/libexec/urlgrabber-ext-down最上面的一行改为#!/usr/bin/python2.7] yum install git Note: ...

  9. struts2 过滤器

    Chain.doFilter的作用就是继续请求的传递,可传递给下一个filter也可传递给目标页面 如左侧传递给filter2,但fiter2使用上面或者下面的方法将倾情重定向到一个新的页面,而不再传 ...

  10. redis07-----Redis持久化配置

    Redis持久化配置 持久化: 即把数据存储于断电后不会丢失的设备中,通常是硬盘. 常见的持久化方式: 主从:通过从服务器保存和持久化,如mongoDB的replication sets配置. 淘宝是 ...