当前位置:网站首页>leetcode:1201. 丑数 III【二分 + 数学 + 容斥原理】
leetcode:1201. 丑数 III【二分 + 数学 + 容斥原理】
2022-08-01 12:46:00 【白速龙王的回眸】

分析
丑树序列不能重复,所以是三个倍数之并
但并不好求,容斥原理展开
利用最小公倍数展开公式成为一个单调不减的fx
fx即为小于等于x的丑树的个数
二分找到第一个使得fx = n的x
这里需要注意一下是小于的话l = mid + 1
大于等于r = mid
细节一下
ac code
class Solution:
def nthUglyNumber(self, n: int, a: int, b: int, c: int) -> int:
# 数学题 + 容斥原理
# 小于等于x的丑数个数设成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
# 二分找到第一个使得f(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
总结
容斥原理简化问题 二分常规解决
边栏推荐
- SCHEMA solves the puzzle
- Complete Raiders of JS Data Type Conversion
- 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
- 如何利用DevExpress控件绘制流程图?看完这篇文章就懂了!
- 字体反爬之好租
- SQL函数 SQUARE
- [Open class preview]: Research and application of super-resolution technology in the field of video image quality enhancement
- 动态库、静态库浅析
- Aeraki Mesh became CNCF sandbox project
- 线上问题排查常用命令,总结太全了,建议收藏!!
猜你喜欢

找出相同属性值的对象 累加数量 汇总

Feign 从注册到调用原理分析

并发编程10大坑,你踩过几个?

MySQL调优

态路小课堂丨浅谈优质光模块需要具备的条件!

10年稳定性保障经验总结,故障复盘要回答哪三大关键问题?|TakinTalks大咖分享

数据湖 delta lake和spark版本对应关系

How to use DevExpress controls to draw flowcharts?After reading this article, you will understand!

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

MarkDown公式指导手册
随机推荐
How do programmers solve online problems gracefully?
LeetCode_动态规划_中等_377.组合总和 Ⅳ
浏览器存储
程序员如何优雅地解决线上问题?
Tencent Cloud Native: Service Mesh Practice of Areaki Mesh in the 2022 Winter Olympics Video Live Application
[5 days countdown] to explore the secret behind the great quality promotion, gift waiting for you to take of $one thousand
ECCV22|只能11%的参数就能优于Swin,微软提出快速预训练蒸馏方法TinyViT
深入理解 Istio —— 云原生服务网格进阶实战
Visualization of lag correlation of two time series data in R language: use the ccf function of the forecast package to draw the cross-correlation function, and analyze the lag correlation according t
R language ggplot2 visualization: use the ggdensity function of the ggpubr package to visualize density plots, use the stat_central_tendency function to add mean vertical lines to the density and cust
深入解析volatile关键字
【面试高频题】难度 1.5/5,二分经典运用题
Js手写函数之new的模拟实现
[Open class preview]: Research and application of super-resolution technology in the field of video quality enhancement
收藏|机械工程师面试常问问题
Apex installation error
[Open class preview]: Research and application of super-resolution technology in the field of video image quality enhancement
一文带你彻底厘清 Kubernetes 中的证书工作机制
如何使用 Authing 单点登录,集成 Discourse 论坛?
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