878. Nth Magical Number
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 <= N <= 10^92 <= A <= 400002 <= 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);
}
};
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的更多相关文章
- [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. ...
- 【leetcode】878. Nth Magical Number
题目如下: 解题思路:本题求的是第N个Magical Number,我们可以很轻松的知道这个数的取值范围 [min(A,B), N*max(A,B)].对于知道上下界求具体数字的题目,可以考虑用二分查 ...
- [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. ...
- (斐波那契总结)Write a method to generate the nth Fibonacci number (CC150 8.1)
根据CC150的解决方式和Introduction to Java programming总结: 使用了两种方式,递归和迭代 CC150提供的代码比较简洁,不过某些细节需要分析. 现在直接运行代码,输 ...
- 【Leetcode_easy】1137. N-th Tribonacci Number
problem 1137. N-th Tribonacci Number solution: class Solution { public: int tribonacci(int n) { ) ; ...
- [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 + ...
- LeetCode.1137-第N个泰波那契数(N-th Tribonacci Number)
这是小川的第409次更新,第441篇原创 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第260题(顺位题号是1137).Tribonacci(泰波那契)序列Tn定义如下: 对于n&g ...
- 1137. N-th Tribonacci Number(Memory Usage: 13.9 MB, less than 100.00% of Python3)
其实思路很简单,套用一下普通斐波那契数列的非递归做法即可,不过这个成绩我一定要纪念一下,哈哈哈哈哈 代码在这儿: class Solution: def tribonacci(self, n: int ...
- 【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 ...
随机推荐
- python各种类型转换
python各种类型转换 学习了:https://blog.csdn.net/shanliangliuxing/article/details/7920400 https://blog.csdn.ne ...
- weexapp 开发流程(三)其他页面创建
1.首页 (1)轮播图 步骤一:创建 轮播图 组件(Slider.vue) src / assets / components / Slider.vue <!-- 轮播图 组件 --> & ...
- 机房收费系统(VB.NET)个人版总结
重构版个人机房收费系统大概从暑假开学開始进行.花了不到一个半月的时间才完毕.以下对我在重构过程中的一写理解. 1.系统设计一个非常重要的目的就是重用.而要做到重用,低耦合是最有效的手段回想一下我们C/ ...
- Android OpenGL ES(六)----进入三维在代码中创建投影矩阵和旋转矩阵
我们如今准备好在代码中加入透视投影了. Android的Matrix类为它准备了两个方法------frustumM()和perspectiveM(). 不幸的是.frustumM()的个缺陷,它会影 ...
- IconTabPageIndicator
https://github.com/msdx/IconTabPageIndicator
- return和exit
return从当前函数返回而exit结束正在运行的程序 示例: [wangml@iZwz976helaylvgqok97prZ testForC]$ ./exit.test q [wangml@iZw ...
- quilt - 制作patch的工具
quilt - 制作patch的工具 在尝试为openwrt做一个patch时,查到这个工具.openwrt官方已经有很详细的文档对步骤进行说明了. quilt并不是专为openwrt的开发工具.qu ...
- CentOS挂载优盘
插入优盘前: [root@centOS5 mnt]# fdisk -l Disk /dev/hdd: bytes heads, sectors/track, cylinders Units = cyl ...
- mysql关闭skip-grant-tables快速重置mysql密码
如果你忘记了mysql密码几乎是没有什么好办法可以直接修改密码了,但我们可以在my.ini把加上skip-grant-tables,然后重启mysql就不需要密码了,这时我们再修改root密码,最后再 ...
- 项目Beta冲刺(团队7/7)
项目Beta冲刺(团队7/7) 团队名称: 云打印 作业要求: 项目Beta冲刺(团队) 作业目标: 完成项目Beta版本 团队队员 队员学号 队员姓名 个人博客地址 备注 221600412 陈宇 ...