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 ...
随机推荐
- maven最小配置
将参与项目开发的开发人员的用户名及邮箱捆绑在一起,在code review是更加方便的进行版本管控: 1.配置user,name和user,email命令: $ git config --global ...
- 【基础练习】【线性DP】codevs3027 线段覆盖2题解
文章被盗还是非常严重,加版权信息 转载请注明出处 [ametake版权全部]http://blog.csdn.net/ametake欢迎来看看 这道题目是线性动归 可是思想和背包有些类似 事实上线性动 ...
- 鼠标滚轮实现图片的缩放-------Day79
今天是7月的最后一天了,不得不说,我定下的七月份剩余几天的计划是完不成了.一则工作确实紧了些,再则没能处理好生活.工作和学习的节奏.这才是人生最大的课题吧.只是也还好.至少自己还在坚持着.事实上真的越 ...
- 嵌入式驱动开发之2440/2410---uboot 移植
http://blog.chinaunix.net/uid-20620288-id-3058904.html
- 字符串函数---strcat()与strncat具体解释及实现
一.strcat()与strncat() strcat():strcat(dest,src); strcat把src所指向的字符加入到dest结尾处(覆盖原dest结尾处的'\0').并 ...
- eclipse 添加库
Window ->Preferences ->Java ->Build Path ->User Libraries New,起个名字,如myLibrary add jars,添 ...
- POJ 1737 Connected Graph(高精度+DP递推)
题面 \(solution:\) 首先做个推销:带负数的压位高精度(加减乘+读写) 然后:由 \(N\) 个节点组成的无向图的总数为: \(2^{N*(N-1)/2}\) (也就是说这个图总共有 \( ...
- 织梦文章分页后文章title的修改使得不一致
织梦Dedecms是一个不错的建站cms系统,最近在用织梦建站的时候发现文章分页后,每个分页都是同一个标题,不利于优化,想在分页后面加上一个数字来进行区别,怎么做呢? 找到include/arc.ar ...
- 解决IE浏览器部分版本不支持background-size属性问题
background-size是CSS3新增的属性,现在有很多浏览器都支持CSS3了.但是IE浏览器有些版本还是不支持,比如IE8,IE9也有些CSS3的属性会支持,但是有些也不支持.在这里就了解一下 ...
- html5--6-40 CSS边框
html5--6-40 CSS边框 实例 div动态阴影 学习要点 掌握CSS边框属性的使用 元素的边框就是围绕元素内容和内边距的一条或多条线. 元素的边框属性: border 简写属性,用于把针对四 ...