当前位置:网站首页>1175. 质数排列 : 乘法原理运用题
1175. 质数排列 : 乘法原理运用题
2022-06-30 11:31:00 【宫水三叶的刷题日记】
题目描述
这是 LeetCode 上的 1175. 质数排列 ,难度为 简单。
Tag : 「数学」、「组合数」、「二分」、「打表」
请你帮忙给从 到 的数设计排列方案,使得所有的「质数」都应该被放在「质数索引」(索引从 开始)上;你需要返回可能的方案总数。
让我们一起来回顾一下「质数」:质数一定是大于 的,并且不能用两个小于它的正整数的乘积来表示。
由于答案可能会很大,所以请你返回答案 模 mod 之后的结果即可。
示例 1:
输入:n = 5
输出:12
解释:举个例子,[1,2,5,4,3] 是一个有效的排列,但 [5,2,3,4,1] 不是,因为在第二种情况里质数 5 被错误地放在索引为 1 的位置上。
示例 2:
输入:n = 100
输出:682289015
提示:
打表 + 二分 + 数学
根据题意,可将问题转换为求 以内的质数个数,记为 ,同时可得非质数个数为 。
质数的放置方案数为 ,而非质数的放置方案数为 ,根据「乘法原理」总的放置方案数为 。
我们可以通过「打表」的方式将 以内的质数预处理到数组 list 中,对于每个 而言,我们找到第一个满足「值小于等于 」的位置,从而得知 范围以内的质数个数。
代码:
class Solution {
static int MOD = (int)1e9+7;
static List<Integer> list = new ArrayList<>();
static {
for (int i = 2; i <= 100; i++) {
boolean ok = true;
for (int j = 2; j * j <= i; j++) {
if (i % j == 0) ok = false;
}
if (ok) list.add(i);
}
}
public int numPrimeArrangements(int n) {
int l = 0, r = list.size() - 1;
while (l < r) {
int mid = l + r + 1 >> 1;
if (list.get(mid) <= n) l = mid;
else r = mid - 1;
}
int a = r + 1, b = n - a;
long ans = 1;
for (int i = b; i > 1; i--) ans = ans * i % MOD ;
for (int i = a; i > 1; i--) ans = ans * i % MOD ;
return (int)ans;
}
}
时间复杂度:二分的复杂度为 ,计算方案数的复杂度为 。整体复杂度为 空间复杂度: ,其中 为 以内的质数个数
打表 + 数学
更进一步,对于特定的 而言,我们在预处理 以内的质数时,已经可以确定在 内有多少个质数,从而省掉二分操作。
使用数组 cnts 记录下不超过当前值范围内质数的个数, 含义为在 范围内质数数量为 。
代码:
class Solution {
static int MOD = (int)1e9+7;
static int[] cnts = new int[110];
static {
List<Integer> list = new ArrayList<>();
for (int i = 2; i <= 100; i++) {
boolean ok = true;
for (int j = 2; j * j <= i; j++) {
if (i % j == 0) ok = false;
}
if (ok) list.add(i);
cnts[i] = list.size();
}
}
public int numPrimeArrangements(int n) {
int a = cnts[n], b = n - a;
long ans = 1;
for (int i = b; i > 1; i--) ans = ans * i % MOD ;
for (int i = a; i > 1; i--) ans = ans * i % MOD ;
return (int)ans;
}
}
时间复杂度: 空间复杂度: ,其中
最后
这是我们「刷穿 LeetCode」系列文章的第 No.1175 篇,系列开始于 2021/01/01,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。
在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。
为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode 。
在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。
更多更全更热门的「笔试/面试」相关资料可访问排版精美的 合集新基地
边栏推荐
- 揭秘得物客服IM全链路通信过程
- Esp32-c3 introductory tutorial IOT part ⑤ - Alibaba cloud Internet of things platform espaliyun RGB LED practical mass production solution
- Multiparty cardinality testing for threshold private set-2021: Interpretation
- 60 divine vs Code plug-ins!!
- What is a wechat applet that will open the door of the applet
- Database transactions
- "New digital technology" completed tens of millions of yuan of a + round financing and built an integrated intelligent database cloud management platform
- Le talent scientifique 丨 dessins animés qu'est - ce qu'erdma?
- Evaluation of IP location query interface Ⅲ
- Let's talk about how to do hardware compatibility testing and quickly migrate to openeuler?
猜你喜欢

科普达人丨漫画图解什么是eRDMA?

19年来最艰难的618,徐雷表达三个谢意

wallys/IPQ8074a/2x(4 × 4 or 8 × 8) 11AX MU-MIMO DUAL CONCURRENT EMBEDDEDBOARD

阿里云李飞飞:中国云数据库在很多主流技术创新上已经领先国外

Digitalization is not a trial, but a wading out of "Xingzhi Digital China" × History of Foxconn

The first batch in China! Alibaba cloud native data Lake products have passed the evaluation and certification of the ICT Institute

对象映射 - Mapping.Mapster
![[IC5000 tutorial] - 01- use daqdea graphical debug to debug C code](/img/54/037b62ff9682ae9fdf861263211043.png)
[IC5000 tutorial] - 01- use daqdea graphical debug to debug C code

Uncover the whole link communication process of customer service im

YOLOv5导出onnx遇到的坑
随机推荐
R语言ggplot2可视化:使用ggplot2可视化散点图、使用scale_size函数配置数据点的大小的(size)度量调整的范围
R语言ggplot2可视化:使用ggplot2可视化散点图、在geom_point参数中设置show_legend参数为FALSE配置不显示图例信息
R语言ggplot2可视化:使用ggplot2可视化散点图、使用scale_x_log10函数配置X轴的数值范围为对数坐标
数据库 级联操作
1175. prime permutation
60 个神级 VS Code 插件!!
R语言ggplot2可视化:使用ggplot2可视化散点图、aes函数中的colour参数指定不同分组的数据点使用不同的颜色显示
Introduction to China Mobile oneos development board
科普達人丨漫畫圖解什麼是eRDMA?
He was the first hero of Shanghai's two major industries, but died silently in regret
R语言去重操作unique duplicate filter
再不上市,旷视科技就熬不住了
HMS Core音频编辑服务3D音频技术,助力打造沉浸式听觉盛宴
数据库 事务
Review the writing software with characteristics
对象映射 - Mapping.Mapster
Database transactions
Esp32-c3 introductory tutorial basic part ⑫ - mass production burning device configuration and serial number, NVS partition confirmation, NVS partition generation program, CSV to bin
wallys/600VX – 2×2 MIMO 802.11ac Mini PCIe Wi-Fi Module, Dual Band, 2,4GHz / 5GHz QCA 9880
Evaluation of IP location query interface Ⅲ