Leetcode-5198 Ugly Number III(丑数 III)
1 #define _for(i,a,b) for(int i = (a);i < b;i ++) 2 #define _rep(i,a,b) for(int i = (a);i > b;i --) 3 #define INF 0x3f3f3f3f 4 #define MOD 1000000007 5 #define pb push_back 6 #define maxn 100003 7 typedef long long ll; 8 class Solution 9 { 10 public: 11 ll kk; 12 ll aa; 13 ll bb; 14 ll cc; 15 ll ab; 16 ll bc; 17 ll ca; 18 ll abc; 19 ll gcd(ll x,ll y) 20 { 21 return y?gcd(y,x%y):x; 22 } 23 ll lcm(long long a, long long b) 24 { 25 return a*b/gcd(a, b); 26 } 27 bool C(ll d) 28 { 29 return (d/aa+d/bb+d/cc-d/(ab)-d/(ca)-d/(bc)+d/(abc))>=kk; 30 } 31 ll solve() 32 { 33 ll lb = 0,ub = 2000000001; 34 while(lb < ub) 35 { 36 ll mid = lb+(ub-lb)/2; 37 if(C(mid)) ub = mid; 38 else lb = mid+1; 39 } 40 return lb; 41 } 42 int nthUglyNumber(int n, int a, int b, int c) 43 { 44 kk = n; 45 aa = a; 46 bb = b; 47 cc = c; 48 ab = lcm(a,b); 49 bc = lcm(b,c); 50 ca = lcm(a,c); 51 abc = lcm(ab,c); 52 return solve(); 53 } 54 };