当前位置:网站首页>LeetCode:1175. 质数排列
LeetCode:1175. 质数排列
2022-07-06 17:39:00 【Vicky__3021】
题目:
1175. 质数排列
请你帮忙给从 1 到 n 的数设计排列方案,使得所有的「质数」都应该被放在「质数索引」(索引从 1 开始)上;你需要返回可能的方案总数。
让我们一起来回顾一下「质数」:质数一定是大于 1 的,并且不能用两个小于它的正整数的乘积来表示。
由于答案可能会很大,所以请你返回答案 模 mod 10^9 + 7 之后的结果即可。
示例 1:
输入:n = 5
输出:12
解释:举个例子,[1,2,5,4,3] 是一个有效的排列,但 [5,2,3,4,1] 不是,因为在第二种情况里质数 5 被错误地放在索引为 1 的位置上。
示例 2:
输入:n = 100
输出:682289015
提示:
- 1 <= n <= 100
解析:
n为1或2时,返回值为1。当n大于2时,通过遍历整除判断该数是否为质数,如果某个数可以整除i,那么i为合数。
代码:
class Solution:
def numPrimeArrangements(self, n: int) -> int:
count1 = 1 # 合数数量
count2 = 1 # 质数数量
ans = 1
if n == 1 or n == 2:
return 1
for i in range(3, n+1):
for j in range(2, i):
if i % j == 0:
count1 += 1
ans = ans * count1 % (7 + 10**9)
break
else:
count2 += 1
ans = ans * count2 % (7 + 10**9)
return ans

边栏推荐
- SuperSocket 1.6 创建一个简易的报文长度在头部的Socket服务器
- Spark TPCDS Data Gen
- Fastdfs data migration operation record
- Install Firefox browser on raspberry pie /arm device
- Installation of gazebo & connection with ROS
- 接收用户输入,身高BMI体重指数检测小业务入门案例
- Wood extraction in Halcon
- 机器学习:随机梯度下降(SGD)与梯度下降(GD)的区别与代码实现。
- [case sharing] basic function configuration of network loop detection
- Come on, don't spread it out. Fashion cloud secretly takes you to collect "cloud" wool, and then secretly builds a personal website to be the king of scrolls, hehe
猜你喜欢

Tensorflow GPU installation

JTAG debugging experience of arm bare board debugging

Force buckle 1037 Effective boomerang

Gazebo的安装&与ROS的连接

【信号与系统】

ClickHouse字段分组聚合、按照任意时间段粒度查询SQL

pytorch之数据类型tensor

go-zero微服务实战系列(九、极致优化秒杀性能)
![[100 cases of JVM tuning practice] 05 - Method area tuning practice (Part 2)](/img/40/dc45df3cd3ee7642277eff899bc6aa.png)
[100 cases of JVM tuning practice] 05 - Method area tuning practice (Part 2)

HMM notes
随机推荐
Installation of gazebo & connection with ROS
ESP Arduino (IV) PWM waveform control output
2022 Google CTF segfault Labyrinth WP
Taro 小程序开启wxml代码压缩
HMM 笔记
自旋与sleep的区别
安全保护能力是什么意思?等保不同级别保护能力分别是怎样?
Metauniverse urban legend 02: metaphor of the number one player
【芯片方案设计】脉搏血氧仪
Your cache folder contains root-owned files, due to a bug in npm ERR! previous versions of npm which
golang中的atomic,以及CAS操作
Windows installation mysql8 (5 minutes)
Let's see through the network i/o model from beginning to end
【js】获取当前时间的前后n天或前后n个月(时分秒年月日都可)
Atomic in golang, and cas Operations
机器学习:随机梯度下降(SGD)与梯度下降(GD)的区别与代码实现。
golang中的Mutex原理解析
实现mysql与ES的增量数据同步
mysql: error while loading shared libraries: libtinfo. so. 5: cannot open shared object file: No such
docker 方法安装mysql