forked from Nimesh-Srivastava/DSA
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0878.cpp
More file actions
35 lines (26 loc) · 694 Bytes
/
0878.cpp
File metadata and controls
35 lines (26 loc) · 694 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
class Solution {
public:
int mod = 1e9 + 7;
int hcf(int a, int b){
if(b == 0)
return a;
return hcf(b, a % b);
}
int lcm(int a, int b){
return (a * b) / hcf(a, b);
}
int nthMagicalNumber(int n, int a, int b) {
long long l = 1;
long long r = 1e17;
while(l < r){
long long mid = l + (r - l)/2;
long long temp = (mid / a) + (mid / b);
temp -= (mid / lcm(a, b));
if(temp < n)
l = mid + 1;
else
r = mid;
}
return r % mod;
}
};