一.问题描述

Return the number of permutations of 1 to n so that prime numbers are at prime indices (1-indexed.)

(Recall that an integer is prime if and only if it is greater than 1, and cannot be written as a product of two positive integers both smaller than it.)

Since the answer may be large, return the answer modulo 10^9 + 7.

Example 1:

Input: n = 5
Output: 12
Explanation: For example [1,2,5,4,3] is a valid permutation, but [5,2,3,4,1] is not because the prime number 5 is at index 1.

Example 2:

Input: n = 100
Output: 682289015

Constraints:

  • 1 <= n <= 100

二.解决思路

其实就是找1~n有多少个质数,然后计算所有质数的全排列和所有非质数的全排列然后相乘求余

本身这个东西是比较简单的,

你可以一个个迭代判断每个数是不是质数,

但是求一定范围内质数个数的最快算法是(除了查表法)Sieve of Eratosthenes(埃拉托色泥筛选法)算法

其实根据定理:一个合数总是可以分解成若干个质数的乘积,那么如果把质数(最初只知道2是质数)的倍数都去掉,那么剩下的就是质数了。

所以我们可以从2开始一个个的把倍数都去掉,直到大于n为止。

给出两种实现

更多leetcode算法题解法请关注我的专栏leetcode算法从零到结束或关注我

欢迎大家一起套路一起刷题一起ac

三.源码

1.一个个的求是否是质数

from functools import reduce
import math
class Solution:def numPrimeArrangements(self, n: int) -> int:cnt_prime=0for i in range(2,n+1):is_prime=1for t in range(2,int(math.sqrt(i))+1):if i%t==0:is_prime=0breakcnt_prime+=is_primeif cnt_prime!=0: # reduce函数用来求阶乘per_prime=reduce(lambda x,y:x*y, range(1,cnt_prime+1))else:per_prime=1return (per_prime*(reduce(lambda x,y:x*y, range(1,n-cnt_prime+1))))%(pow(10,9)+7)

2.埃拉托色泥筛选法

from functools import reduce
class Solution:def numPrimeArrangements(self, n: int) -> int:is_prime=[1]*(n+1)is_prime[0],is_prime[1],=0,0p=2while p*p<=n:if is_prime[p]:for t in range(p*p,n+1,p):is_prime[t]=0p+=1cnt_prime=sum(is_prime)if cnt_prime!=0:per_prime=reduce(lambda x,y:x*y, range(1,cnt_prime+1))else:per_prime=1return (per_prime*(reduce(lambda x,y:x*y, range(1,n-cnt_prime+1))))%(10**9+7)

leetcode 1175. Prime Arrangements 解法 python相关推荐

  1. LeetCode 31. Next Permutation-- Python 解法--数学题--比当前数大的最小的数

    LeetCode 31. Next Permutation-- Python 解法–数学题–比当前数大的最小的数 此文首发于我的个人博客:LeetCode 31. Next Permutation-- ...

  2. LeetCode 字符串简单部分 算法 python实现

    ''' #2018-06-02 June Saturday the 22 week, the 153 day SZ LeetCode 字符串简单部分 算法 python实现 https://leetc ...

  3. LeetCode 75. Sort Colors (python一次遍历,模拟三路快排)

    LeetCode 75. Sort Colors (python一次遍历,模拟三路快排) 题目分析: 本题需要实现数字只包含0,1,2的排序,并且要求一次遍历. 由于只用把数字隔离开,很容易想到快排的 ...

  4. 最大质因子递归解法python

    最大质因子的递归解法&Python: 问题描述: 求一个任意正整数的最大质因子,呈上代码及注释: # 最大质因子 def calc(x:int)->int:# 函数参数类型设置以及返回值 ...

  5. LeetCode 518 Coin Change 2 (python)

    目录 LeetCode 518 Coin Change 2 (python) Code 欢迎一起来参与leetcode刷题项目 LeetCode 518 Coin Change 2 (python) ...

  6. leetcode 1556. Thousand Separator(python)

    leetcode 1556. Thousand Separator(python) 描述 Given an integer n, add a dot (".") as the th ...

  7. LeetCode 264. Ugly Number II--C++,Python解法

    题目地址:Ugly Number II - LeetCode Write a program to find the n-th ugly number. Ugly numbers are positi ...

  8. LeetCode 20. Valid Parentheses--笔试题--Python解法

    题目地址:Valid Parentheses - LeetCode Given a string containing just the characters '(', ')', '{', '}', ...

  9. LeetCode 2. Add Two Numbers--C++,Python解法--面试算法题

    题目地址:Add Two Numbers - LeetCode You are given two non-empty linked lists representing two non-negati ...

  10. LeetCode 489. Robot Room Cleaner--C++,Python解法

    题目地址:Robot Room Cleaner - LeetCode Given a robot cleaner in a room modeled as a grid. Each cell in t ...

最新文章

  1. PMP-【第1章 引论】-2020-12-29(25页-34页)
  2. 程序员应知——团队精神(转)
  3. http预请求options
  4. python 检查文件是否存在_Python中如何判断文件是否存在?
  5. Eigen(3)矩阵Matrix及其简单操作
  6. c语言实验题水仙花数5359,《C语言程序设计》实验报告(实验1-12).doc
  7. 软件工程——成品展示
  8. mysql无法启动服务,错误1067
  9. 【数学建模】7 线性规划及例题详解
  10. 苹果开场铃声 android,推荐几款免费安卓、苹果手机铃声剪辑软件?
  11. 完美数Python解法
  12. npm i 安装依赖 报错 npm ERR! code EINTEGRITY npm ERR! sha512-*****==.(12008 bytes) 问题处理
  13. python群发邮件 不进垃圾箱_邮件群发不进垃圾箱
  14. kafka的offset理解
  15. 8位数字的正则表达式
  16. 鸿蒙kal内核,鸿蒙OS发布!官方打脸安卓换壳:华为自研Open鸿蒙内核不兼容安卓...
  17. mssql sqlserver 添加表注释和添加列注释
  18. uni-app实现验证码登录-云之讯
  19. Docker 部署SpringBoot应用
  20. 彻底搞懂 RxJava

热门文章

  1. U盘无法与计算机连接,连接电脑后为什么u盘不显示 连接电脑后u盘不显示原因【图文】...
  2. u盘在电脑上读不出来?从这几方面排查
  3. ROS多设备组网(WSL+miniPC+Nv Orin)
  4. Kali局域网断网攻击
  5. 按字寻址和按字节寻址
  6. 【Codecs系列】CABAC熵编码详解
  7. [附源码]Python计算机毕业设计大学生学科竞赛管理系统
  8. python交易是什么意思_py交易什么梗?起源及流行原因
  9. html的excel表格自动换行,excel自动换行总结
  10. Linux进阶_加密和安全