当前位置:网站首页>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 .
边栏推荐
- The difference between drop, truncate and delete
- 单细胞论文记录(part14)--CoSTA: unsupervised convolutional neural network learning for ST analysis
- 用蒙特卡洛法求圆周率pi
- 【NAND文件系统】UBIFS介绍
- Breakthrough in rich device platform: dayu200 based on rk3568 enters the openharmony 3.1 release trunk
- 类加载内存分析
- Markdown中插入类图(classDiagram)
- reduce_sum()中的reduction_indices
- CGIC文件上传----菜鸟笔记
- D3D learning notes (1) - Introduction to the use conditions of autodraw at so stage
猜你喜欢

PyG教程(7):剖析邻域聚合

Modeling and Simulation of Radar Seeker Servo System

Subqueries in sqlserver

Creating GLSL Shaders at Runtime in Unity3D

什么是JUC

Single cell paper record (Part14) -- costa: unsupervised revolutionary neural network learning for St analysis

ReadWriteLock

Use of idea plug-in EASYCODE

五大常考SQL面试题

D3D learning notes (1) - Introduction to the use conditions of autodraw at so stage
随机推荐
生信可视化(part2)--箱线图
TCP connection details
ForkJoinPool
五大常考SQL面试题
PIR控制器调节器并网逆变器电流谐波抑制策略
单细胞论文记录(part6)--SpaGE: Spatial Gene Enhancement using scRNA-seq
【云计算重点复习】
【Rust笔记】01-基本类型
Single cell paper record (Part14) -- costa: unsupervised revolutionary neural network learning for St analysis
tab[i = (n - 1) & hash] 的详细解读
Flink核心功能和原理
878. 第 N 个神奇数字 数学+二分
Shengxin visualization (Part3) -- violin diagram
动态创建对象执行方法
Unity encrypts ASE game data
常用的辅助类—(重点)
Research on dynamics and control of single ball robot
vcpkg:If you are sure you want to rebuild the above packages, run the command with the --recurse opt
Machine learning concept sorting (no formula)
Markdown中插入类图(classDiagram)