当前位置:网站首页>878. the nth magic number math + two points
878. the nth magic number math + two points
2022-06-22 06:15:00 【Empress Yu】
878. The first N A magic number
If a positive integer can be
aorbto be divisible by , So it's amazing .Given three integers
n,a,b, Back to pagenA magic number . Because the answer can be big , So return the answer Yes109 + 7modulus After the value of .Example 1:
Input :n = 1, a = 2, b = 3 Output :2Example 2:
Input :n = 4, a = 2, b = 3 Output :6Tips :
1 <= n <= 10^92 <= a, b <= 4 * 10^4source : Power button (LeetCode)
link :https://leetcode.cn/problems/nth-magical-number
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
The result of doing the question
success
Method : mathematics + Two points
1. Find the greatest common divisor
2. Every time the greatest common divisor is reached , Namely from a The number of arrivals + from b Number of arrivals - lcm frequency
3. That's about the last common multiple , To the next common multiple , such as 6,2,3 ,2 and 3 The minimum common multiple is 6, Every time we arrive 6 Just pass 3 individual 2 Multiple and 2 individual 3 Multiple , among 6 It's public , Subtract 1 individual , Every time 6, Just consume 4 Number , So you can be sure ,n=6 It's through 1 individual Least common multiple period , That is to say n/4*6=6, That is, about 6 To 12 Between , You can get the answer in this part .
class Solution {
public int nthMagicalNumber(int n, int a, int b) {
long lcm = lcm(a,b);
long time = lcm/a+lcm/b-1;
long left = n/time*lcm;
long right = (n/time+1)*lcm;
while(left<right){
long mid = (right-left)/2+left;
long cnt = mid/a+mid/b-mid/lcm;
if(cnt<n){
left = mid+1;
}else{
right = mid;
}
}
return (int) (right%(long)(1e9+7));
}
private long lcm(long a, long b){
long gcd = gcd(a,b);
return a/gcd*b/gcd*gcd;
}
private long gcd(long a, long b){
return b== 0?a:gcd(b,a%b);
}
}Time complexity :O(log(max(a,b)))
Spatial complexity :O(1)
other , If a,b smaller , n Large enough ( Intermediate calculation exceeds long) Under the circumstances , How to deal with it ?
Consider recording a cycle , Instead of counting the times directly , Front part , The sum of the remainder can be calculated directly to the common multiple , The last part can be reduced to the first cycle calculation , Then add the previous remainder to find the remainder .
边栏推荐
- 单细胞论文记录(part9)--Spatial charting of single-cell transcriptomes in tissues
- 878. 第 N 个神奇数字 数学+二分
- Unity encrypts ASE game data
- OSG compiling osgqt
- 牛客-TOP101-BM27
- 单细胞论文记录(part10)--Computational challenges and opportunities in SRT data
- 单细胞论文记录(part11)--ClusterMap for multi-scale clustering analysis of spatial gene expression
- Empirical mode decomposition (EMD) and Hilbert Huang transform (HHT)
- [Examen des points clés de l'informatique en nuage]
- PIR控制器调节器并网逆变器电流谐波抑制策略
猜你喜欢
随机推荐
舰载机自动着舰控制系统研究
从转载阿里开源项目 Egg.js 技术文档引发的“版权纠纷”,看宽松的 MIT 许可该如何用?
Generics in C #
生信可视化(part2)--箱线图
postgresql数据库中根据某个字段判断存在则更新(update)操作,不存在则插入(insert)
不务正业系列7:老照片去除斑点手法
W800 chip platform enters openharmony backbone
生信可视化(part1)--柱状图
Bat common batch script record
动态创建对象执行方法
Detailed interpretation of tab[i = (n - 1) & hash]
Lock锁(重点)
StopWatch的使用
pip升级难题(已解决)You are using pip version 19.0.3, however version 22.1.2 is available.
单球机器人动力学与控制研究
[technical notes]
Single precision, double precision and precision (Reprint)
GeoSwath plus 技术和数据采集处理
常用的辅助类—(重点)
性能对比分析









