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 };

原文地址:https://www.cnblogs.com/Asurudo/p/11566946.html

时间: 2024-09-29 22:57:45

Leetcode-5198 Ugly Number III(丑数 III)的相关文章

LeetCode OJ:Ugly Number(丑数)

Write a program to check whether a given number is an ugly number. Ugly numbers are positive numbers whose prime factors only include 2, 3, 5. For example, 6, 8 are ugly while 14 is not ugly since it includes another prime factor 7. Note that 1 is ty

LeetCode OJ 之 Ugly Number (丑数)

题目: Write a program to check whether a given number is an ugly number. Ugly numbers are positive numbers whose prime factors only include 2, 3, 5. For example, 6, 8 are ugly while 14 is not ugly since it includes another prime factor 7. Note that 1 i

[LeetCode] 248. Strobogrammatic Number III 对称数III

A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside down). Write a function to count the total strobogrammatic numbers that exist in the range of low <= num <= high. For example,Given low = "50&qu

[LeetCode] 260. Single Number III 单独数 III

Given an array of numbers nums, in which exactly two elements appear only once and all the other elements appear exactly twice. Find the two elements that appear only once. Example: Input: [1,2,1,3,2,5] Output: [3,5] Note: The order of the result is

[LeetCode] 247. Strobogrammatic Number II 对称数II

A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside down). Find all strobogrammatic numbers that are of length = n. For example,Given n = 2, return ["11","69","88","96"

poj1338 Ugly Numbers(丑数模拟)

转载请注明出处:http://blog.csdn.net/u012860063?viewmode=contents 题目链接:http://poj.org/problem?id=1338 Description Ugly numbers are numbers whose only prime factors are 2, 3 or 5. The sequence 1, 2, 3, 4, 5, 6, 8, 9, 10, 12, ... shows the first 10 ugly number

&amp;lt;LeetCode OJ&amp;gt; 26 / 264 / 313 Ugly Number (I / II / III)

Write a program to check whether a given number is an ugly number. Ugly numbers are positive numbers whose prime factors only include 2, 3, 5. For example, 6, 8 are ugly while 14 is not ugly since it includes another prime factor 7. Note that 1 is ty

[LeetCode]70. Ugly Number II第N个丑数

Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose prime factors only include 2, 3, 5. For example, 1, 2, 3, 4, 5, 6, 8, 9, 10, 12 is the sequence of the first 10 ugly numbers. Note that 1 is typically treated as an

[LeetCode] Ugly Number II 丑陋数之二

Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose prime factors only include 2, 3, 5. For example, 1, 2, 3, 4, 5, 6, 8, 9, 10, 12 is the sequence of the first 10 ugly numbers. Note that 1 is typically treated as an