Leetcode(878)-第 N 个神奇数字
如果正整数可以被 A 或 B 整除,那么它是神奇的。
返回第 N 个神奇数字。由于答案可能非常大,返回它模 10^9 + 7 的结果。
示例 1:
输入:N = 1, A = 2, B = 3
输出:2
示例 2:
输入:N = 4, A = 2, B = 3
输出:6
示例 3:
输入:N = 5, A = 2, B = 4
输出:10
示例 4:
输入:N = 3, A = 6, B = 4
输出:8
提示:
1 <= N <= 10^92 <= A <= 400002 <= B <= 40000
思路:我一开始用了很原始的方式,将n从1开始加,判断是否可以被两个数的其中一个整除,如果可以就打入vector中,直到vector中的元素个数为N。不过这种方式不涉及脑子的,结果肯定是超出时间限制。
那么有没有什么好的方法可以解决这个问题。我后来也想过不能每次加1,应该从最大公约最小公倍数之类的入手,但是最终没能完整实现,下面是大神的解法
int gcd(int a,int b)//求最大公约数
{
return b==0?a:gcd(b,a%b);
}
int nthMagicalNumber(int N, int A, int B)
{
long long low=0,high=2000000000000000000L;
int g=A*B/gcd(A,B);
while(low<high)
{
long long mid=(low+high)/2;
long long t=mid/A+mid/B-mid/g;
if(t<N)
{
low=mid+1;
}
else
high=mid;
}
return (int)(high%1000000007);
}
建立一个大区间,通过二分法来求解,大区间的mid,可以知道mid左边有mid/A个A,mid/B个B ,mid/G个AB的最小公倍数,mid/A+mid/B-mid/g就代表mid左边有多少个满足条件的数。然后二分法求解,直到找到一定个数的满足条件的数。
Leetcode(878)-第 N 个神奇数字的更多相关文章
- [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. ...
- [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. ...
- LeetCode:移除K位数字【402】
LeetCode:移除K位数字[402] 题目描述 给定一个以字符串表示的非负整数 num,移除这个数中的 k 位数字,使得剩下的数字最小. 注意: num 的长度小于 10002 且 ≥ k. nu ...
- Leetcode之二分法专题-374. 猜数字大小(374. Guess Number Higher or Lower)
Leetcode之二分法专题-374. 猜数字大小(374. Guess Number Higher or Lower) 我们正在玩一个猜数字游戏. 游戏规则如下:我从 1 到 n 选择一个数字. 你 ...
- [LeetCode] Convert a Number to Hexadecimal 数字转为十六进制
Given an integer, write an algorithm to convert it to hexadecimal. For negative integer, two’s compl ...
- [LeetCode] Single Number III 单独的数字之三
Given an array of numbers nums, in which exactly two elements appear only once and all the other ele ...
- [LeetCode] Bitwise AND of Numbers Range 数字范围位相与
Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of all numbers ...
- [LeetCode] Single Number II 单独的数字之二
Given an array of integers, every element appears three times except for one. Find that single one. ...
- [LeetCode] Palindrome Number 验证回文数字
Determine whether an integer is a palindrome. Do this without extra space. click to show spoilers. S ...
随机推荐
- Redis-第五章节-8种数据类型
目录 一.Redis对key的操作 二.五种数据类型 String类型 List(集合) Set(集合) Hash(哈希) Zset(有序集合) 三.三种特殊数据类型 geospatial(地理位置) ...
- 10_1_OS模块
1.常用函数目录 函数名 功能 os.name 指示用户正在使用的工作平台 os.getcwd ( ) 获取当前的工作目录 os.listdir ( ) 返回指定目录下的所有文件和目录名 os.rem ...
- FLask之视图
视图 1 FBV def index(): return render_template('index.html') app.add_url_rule('/index', 'index', index ...
- CentOS7,非LVM根分区扩容步骤:
1.查看现有的分区大小 非LVM分区,目前磁盘大小为40G,根分区总容量为40G,(是自定义分区安装的) 2.关机增加磁盘大小至100G 如果你们是vmwaer虚拟软件安装的那如下入扩容: 3.查看磁 ...
- Failed to start LSB: starts php-fpm
跟nginx一样都是进程占用,记录下 [root@localhost pazzn]# systemctl status php-fpm.service ● php-fpm-72.service - L ...
- Tensorflow-交叉熵&过拟合
交叉熵 二次代价函数 原理 缺陷 假如我们目标是收敛到0.A点为0.82离目标比较近,梯度比较大,权值调整比较大.B点为0.98离目标比较远,梯度比较小,权值调整比较小.调整方案不合理. 交叉熵代价函 ...
- 实用 nginx.conf 用法大全
服务器拒绝非GET方式请求保障安全性,因为 DELETE.POST.PUT 是可以修改数据的. Nginx 解决方案 在 nginx.conf 配置文件的网站配置区域中添加如下代码片段: 非 GET ...
- 分布式跟踪的一个流行标准是OpenTracing API,该标准的一个流行实现是Jaeger项目。
https://github.com/jaegertracing/jaeger https://mp.weixin.qq.com/s/-Tn2AgyHoq8pwMun8JHcGQ Jaeger的深入分 ...
- new() 和 make() 的区别 var arr1 = new([5]int) var arr2 [5]int
Effective Go - The Go Programming Language https://golang.org/doc/effective_go.html#allocation_new A ...
- Redis 常见问题总结
1. 简单介绍一下 Redis 呗! 简单来说 Redis 就是一个使用 C 语言开发的数据库,不过与传统数据库不同的是 Redis 的数据是存在内存中的 ,也就是它是内存数据库,所以读写速度非常快, ...