当前位置:网站首页>leetcode: 1201. Ugly Number III [Dichotomy + Mathematics + Inclusion and Exclusion Principle]
leetcode: 1201. Ugly Number III [Dichotomy + Mathematics + Inclusion and Exclusion Principle]
2022-08-01 13:02:00 【Looking Back at the White Speed Dragon King】

分析
Ugly tree sequences cannot be repeated,So it is the union of three multiples
But it's not good,The principle of inclusion and exclusion unfolds
Expansion of the formula using the least common multiple becomes a monotonic non-decreasing onefx
fxThat is, less than or equal toxnumber of ugly trees
Divide to find the first makefx = n的x
It should be noted here that it is less thanl = mid + 1
大于等于r = mid
details
ac code
class Solution:
def nthUglyNumber(self, n: int, a: int, b: int, c: int) -> int:
# 数学题 + 容斥原理
# 小于等于xThe number of ugly numbers is set to f(x)
# 两个数的最小公倍数
def lcm(a, b):
return (a * b) // gcd(a, b)
# 三个数的最小公倍数
def lcm3(a, b, c):
return lcm(lcm(a, b), c)
# 容斥原理:|A or B or C| = |A| + |B| + |C| - |A and B| - |B and C| - |C and A| + |A and B and C|
def f(x):
f1 = x // a + x // b + x // c
f2 = x // lcm(a, b) + x // lcm(b, c) + x // lcm(c, a)
f3 = x // lcm3(a, b, c)
return f1 - f2 + f3
# Divide to find the first makef(x) = n的x
# 因为fx单调不减
l, r = 1, 2 * 10 ** 9
while l < r:
mid = (l + r) // 2
if f(mid) < n:
l = mid + 1
else:
r = mid
return l
总结
The principle of inclusion and exclusion simplifies the problem Dichotomous solution
边栏推荐
- This article will take you to thoroughly clarify the working mechanism of certificates in Isito
- Deep understanding of Istio - advanced practice of cloud native service mesh
- AI目标分割能力,无需绿幕即可实现快速视频抠图
- SQL functions STR
- R language ggplot2 visualization: use ggpubr package ggscatter function visualization scatterplot, use xscale wasn't entirely specified X axis measurement adjustment function, set the X coordinate for
- SQL function SQUARE
- STM32 CAN过滤器配置详解
- 通讯录(静态版)(C语言)(VS)
- SCHEMA solves the puzzle
- 测试发文
猜你喜欢
![[Open class preview]: Research and application of super-resolution technology in the field of video image quality enhancement](/img/fc/cd859efa69fa7b45f173de74c04858.png)
[Open class preview]: Research and application of super-resolution technology in the field of video image quality enhancement

Audio and Video Technology Development Weekly | 256

【讲座分享】“营收“看金融

Pytest e-commerce project combat (below)

【StoneDB Class】Introduction Lesson 2: Analysis of the Overall Architecture of StoneDB

The CAN communication standard frame and extended frame is introduced

【StoneDB Class】入门第二课:StoneDB 整体架构解析

How do programmers solve online problems gracefully?
![[5 days countdown] to explore the secret behind the great quality promotion, gift waiting for you to take of $one thousand](/img/de/1e6069e84183d1400c90a6ec574f72.png)
[5 days countdown] to explore the secret behind the great quality promotion, gift waiting for you to take of $one thousand

并发编程10大坑,你踩过几个?
随机推荐
Fault 007: The dexp derivative is inexplicably interrupted
Complete Raiders of JS Data Type Conversion
Data frame and remote frame of CAN communication
The CAN communication standard frame and extended frame is introduced
How much do you know about Amazon reviews?
Aeraki Mesh 加入 CNCF 云原生全景图
HMS Core音频编辑服务音源分离与空间音频渲染,助力快速进入3D音频的世界
Grafana 9.0 released, Prometheus and Loki query builders, new navigation, heatmap panels and more!
动态库、静态库浅析
NebulaGraph v3.2.0 性能报告
什么是一致性哈希?可以应用在哪些场景?
How to Integrate Your Service Registry with Istio?
达梦更换正式授权dm.key
如何将第三方服务中心注册集成到 Istio ?
R language fitting ARIMA model: use the auto.arima function in the forecast package to automatically search for the best parameter combination, model order (p, d, q), set the seasonal parameter to spe
PanGu-Coder:函数级的代码生成模型
重磅消息 | Authing 实现与西门子低代码平台的集成
数据湖 delta lake和spark版本对应关系
快速幂---学习笔记
AI目标分割能力,无需绿幕即可实现快速视频抠图