如果正整数可以被 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. 1 <= N <= 10^9
  2. 2 <= A <= 40000
  3. 2 <= 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 个神奇数字的更多相关文章

  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. [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.  ...

  3. LeetCode:移除K位数字【402】

    LeetCode:移除K位数字[402] 题目描述 给定一个以字符串表示的非负整数 num,移除这个数中的 k 位数字,使得剩下的数字最小. 注意: num 的长度小于 10002 且 ≥ k. nu ...

  4. Leetcode之二分法专题-374. 猜数字大小(374. Guess Number Higher or Lower)

    Leetcode之二分法专题-374. 猜数字大小(374. Guess Number Higher or Lower) 我们正在玩一个猜数字游戏. 游戏规则如下:我从 1 到 n 选择一个数字. 你 ...

  5. [LeetCode] Convert a Number to Hexadecimal 数字转为十六进制

    Given an integer, write an algorithm to convert it to hexadecimal. For negative integer, two’s compl ...

  6. [LeetCode] Single Number III 单独的数字之三

    Given an array of numbers nums, in which exactly two elements appear only once and all the other ele ...

  7. [LeetCode] Bitwise AND of Numbers Range 数字范围位相与

    Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of all numbers ...

  8. [LeetCode] Single Number II 单独的数字之二

    Given an array of integers, every element appears three times except for one. Find that single one. ...

  9. [LeetCode] Palindrome Number 验证回文数字

    Determine whether an integer is a palindrome. Do this without extra space. click to show spoilers. S ...

随机推荐

  1. luoguP2016 战略游戏

    题目描述 Bob喜欢玩电脑游戏,特别是战略游戏.但是他经常无法找到快速玩过游戏的办法.现在他有个问题.他要建立一个古城堡,城堡中的路形成一棵树.他要在这棵树的结点上放置最少数目的士兵,使得这些士兵能了 ...

  2. JD6621快速充电协议芯片,带有PPS 控制器的USB-PD3.0

    描述 JD6621是高度集成的USB供电(PD)控制器,支持USB PD 3.0 ,该USB PD 3.0 具有针对USBType-C下游接口(源)设计的可编程电源(PPS)规范.它监视CC引脚以检测 ...

  3. 干电池升压IC

    1, 干电池升压IC                         升压输出3V,3,3V,5V等3V-5V可调   2, 单节锂电池升压IC                     升压输出4.2 ...

  4. 同步与异步 Python 有何不同?

    你是否听到人们说过,异步 Python 代码比"普通(或同步)Python 代码更快?果真是那样吗? 1 "同步"和"异步"是什么意思? Web 应用 ...

  5. 用CSS制做一个三角形!

    用CSS制做一个三角形! <style> .outer { width: 0; height: 0; border-left: 10px solid transparent; border ...

  6. .net core 不同地区时间相互转换

    .net core 不同地区时间相互转换 //韩国时间转换成当前时间 //value=需要转换的时间 //Korea Standard Tim 韩国时间 //China Standard Time 中 ...

  7. fastjson的deserializer的主要优化算法 漏洞

    JSON最佳实践 | kimmking's blog http://kimmking.github.io/2017/06/06/json-best-practice/ Fastjson内幕 Java综 ...

  8. Linux常用命令:文件操作命令

    Linux系统命令主要包括文件操作.网络命令和性能命令,本文介绍常用文件操作命令. 修改文件属性 文件类型: 普通文件:- 目录文件:d 块设备文件:b,硬盘 字符设备: c,串行端口的接口设备,例如 ...

  9. is == id ,编码

    一. id 查询内存地址. # name = 'alex' # print(id(name)) # name1 = 'alex' # name2 = 'alex' # print(name1 == n ...

  10. Grafana Prometheus系统监控Redis服务

    Grafana Prometheus系统监控Redis服务 一.Grafana Prometheus系统监控Redis服务 1.1流程 1.2安装redis_exporter 1.3配置prometh ...