-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution1201.cs
54 lines (46 loc) · 1.23 KB
/
Solution1201.cs
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
using System.Text;
using LeetCode.DataStructures;
namespace LeetCode.Solutions;
public class Solution1201
{
/// <summary>
/// 1201. Ugly Number III - Medium
/// <a href="https://leetcode.com/problems/ugly-number-iii">See the problem</a>
/// </summary>
public int NthUglyNumber(int n, int a, int b, int c)
{
var lcmAB = LCM(a, b);
var lcmAC = LCM(a, c);
var lcmBC = LCM(b, c);
var lcmABC = LCM(lcmAB, c);
var left = 1;
var right = int.MaxValue;
while (left < right)
{
var mid = left + (right - left) / 2;
if (CountUglyNumbers(mid, a, b, c) < n)
left = mid + 1;
else
right = mid;
}
return left;
}
private static long CountUglyNumbers(long x, long a, long b, long c)
{
return x / a + x / b + x / c - x / LCM(a, b) - x / LCM(a, c) - x / LCM(b, c) + x / LCM(a, LCM(b, c));
}
private static long GCD(long a, long b)
{
while (b != 0)
{
var temp = b;
b = a % b;
a = temp;
}
return a;
}
private static long LCM(long a, long b)
{
return a / GCD(a, b) * b;
}
}