1
// This is a typical Binary Search Problem Here I did Binary Search and2
// Optimized my lcm function a lot. Here Number of Ugly numbers for any number3
// is that number/a + that number/b + that number/c + that number/lcm(a,b,c) -4
// that number/lcm(a,b) - that number/lcm(b,c) - that number/(a,c) and howzz5
// that?? See Lets suppose that number is 17 for which you are checking values6
// and a = 2 , b=3 and c= 4 now figure out all the possible values for a =7
// 2,4,6,8,10,12,14,16 b = 3,6,9,12,15 c = 4,8,12,16 Now if we add them all we8
// can see 4,6,8,16 are coming twice and 12 is coming thrice so we do lcm(2,3) =9
// 6 then we are basically multiple occurance of numbers divisible by 6 simlarly10
// for lcm(2,4) & lcm(3,4) but any number which is divisble by all three of them11
// we have deleted it 3 times we need at least so we are adding numbers which12
// are divisble by lcm(2,3,4) which is 12 here So if suppose we are countering13
// more numbers than n then h = mid-1 we need to move backward else we need to23
return (a / gcd(a, b)) * b;25
bool check(ll mid, int a, int b, int c, int n) {30
ll k = mid / a + mid / b + mid / c + mid / j4 - (mid / j1 + mid / j2 + mid / j3);33
bool check1(ll mid, int a, int b, int c, int n) {38
ll k = mid / a + mid / b + mid / c + mid / j4 - (mid / j1 + mid / j2 + mid / j3);41
int nthUglyNumber(int n, int a, int b, int c) {42
ll l = min(a, min(b, c));45
ll mid = l + (h - l) / 2;46
if (check(mid, a, b, c, n) && (mid % a == 0 || mid % b == 0 || mid % c == 0)) {49
if (check1(mid, a, b, c, n)) {