当前位置:网站首页>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
边栏推荐
- table表格设置圆角
- Openjudge noi 1.7 10: simple password
- Transformation transformation operator
- Eventbus source code analysis
- Data type of pytorch tensor
- Supersocket 1.6 creates a simple socket server with message length in the header
- 【案例分享】网络环路检测基本功能配置
- NEON优化:关于交叉存取与反向交叉存取
- [batch dos-cmd command - summary and summary] - string search, search, and filter commands (find, findstr), and the difference and discrimination between find and findstr
- The cost of returning tables in MySQL
猜你喜欢
【案例分享】网络环路检测基本功能配置
Build your own website (17)
微信公众号发送模板消息
Segmenttree
黑马笔记---创建不可变集合与Stream流
Maidong Internet won the bid of Beijing life insurance to boost customers' brand value
[100 cases of JVM tuning practice] 04 - Method area tuning practice (Part 1)
golang中的Mutex原理解析
[case sharing] basic function configuration of network loop detection
动态规划思想《从入门到放弃》
随机推荐
MySQL script batch queries all tables containing specified field types in the database
[batch dos-cmd command - summary and summary] - string search, search, and filter commands (find, findstr), and the difference and discrimination between find and findstr
"Exquisite store manager" youth entrepreneurship incubation camp - the first phase of Shunde market has been successfully completed!
Realize incremental data synchronization between MySQL and ES
Eventbus source code analysis
Your cache folder contains root-owned files, due to a bug in npm ERR! previous versions of npm which
Analysis of mutex principle in golang
7.6模拟赛总结
docker 方法安装mysql
身体质量指数程序,入门写死的小程序项目
线段树(SegmentTree)
Niuke cold training camp 6B (Freund has no green name level)
NEON优化:矩阵转置的指令优化案例
Dynamic planning idea "from getting started to giving up"
ARM裸板调试之JTAG原理
Deep learning framework TF installation
Oracle: Practice of CDB restricting PDB resources
from . cv2 import * ImportError: libGL. so. 1: cannot open shared object file: No such file or direc
[Niuke] [noip2015] jumping stone
Oracle:CDB限制PDB资源实战