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. 区间DP与贪心算法的联系(uav Cutting Sticks &amp;&amp; poj Fence Repair(堆的手工实现))

    由于,这两题有着似乎一样的解法所以将其放在一起总结比較,以达到更好的区分二者的差别所在. 一.区间DP uva的Cutting Sticks是一道典型的模板题. 题目描写叙述: 有一根长度为l的木棍, ...

  2. SQL server 子查询 链接查询

    数据库 if while else 的使用 数据库运算符优先级

  3. Hibernate 配置C3P0 连接池

    第一步在hibernate.cfg.xml配置 <!-- 连接池 --> <property name="hibernate.connection.provider_cla ...

  4. 李洪强iOS开发之 - enum与typedef enum的用法

    李洪强iOS开发之 - enum与typedef enum的用法 01 - 定义枚举类型 上面我们就在ViewController.h定义了一个枚举类型,枚举类型的值默认是连续的自然数,例如例子中的T ...

  5. Cocos2d-x Vector——vector iterators incompatible

    ***************************************转载请注明出处:http://blog.csdn.net/lttree************************** ...

  6. node.js内存泄露问题记录

    先说一下.事情的来龙去脉. 公司开发一款游戏棋牌游戏,服务端的开发是IO密集型,开发的时候,考虑过使用python,java,node.js. 终于选择了node.js(node.js宣传的杀手功能. ...

  7. activity栈管理的3种方式

    一.背景 在android开发过程最经常使用的组件非activity莫属. 通过分析activity的各种跳转,执行同学能够分析用户的各种行为.更重要的一点是在做插件化的过程中,我们经常会对activ ...

  8. 第一讲:使用html5——canvas绘制奥运五环

    <html> <head> <title>初识canvas</title> </head> <body> <canvas ...

  9. windows下安装RubbitMq

    1.下载 下载 rabbitMQ :http://www.rabbitmq.com/download.html,安装rabbitmq需要erlang,下载erlang:http://www.erlan ...

  10. jquery和CSS3带倒影的3D万花筒旋转动画特效效果演示

    <!DOCTYPE html> <html> <head> <title></title> <meta charset='utf-8' ...