当前位置:网站首页>LeetCode:1175. Prime permutation
LeetCode:1175. Prime permutation
2022-07-07 01:24:00 【Vicky__ three thousand and twenty-one】
subject :
1175. Permutation of prime numbers
Please help me to 1 To n Number design arrangement scheme , Make all of 「 Prime number 」 Should be placed in 「 Prime index 」( Index from 1 Start ) On ; You need to return the total number of possible solutions .
Let's review 「 Prime number 」: The prime number must be greater than 1 Of , And it cannot be expressed by the product of two positive integers less than it .
Because the answer could be big , So please return to the answer model mod 10^9 + 7 Then the result is .
Example 1:
Input :n = 5
Output :12
explain : for instance ,[1,2,5,4,3] Is an effective arrangement , but [5,2,3,4,1] No , Because in the second case, prime numbers 5 It is wrongly placed in the index as 1 Location .
Example 2:
Input :n = 100
Output :682289015
Tips :
- 1 <= n <= 100
analysis :
n by 1 or 2 when , The return value is 1. When n Greater than 2 when , Determine whether the number is a prime number by traversing and dividing , If a number can be divisible i, that i As composite .
Code :
class Solution:
def numPrimeArrangements(self, n: int) -> int:
count1 = 1 # Combined number
count2 = 1 # Prime quantity
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

边栏推荐
猜你喜欢

微信公众号发送模板消息

Force buckle 1037 Effective boomerang

HMM 笔记

Lldp compatible CDP function configuration

Your cache folder contains root-owned files, due to a bug in npm ERR! previous versions of npm which

UI control telerik UI for WinForms new theme - vs2022 heuristic theme

免费白嫖的图床对比

Installation of gazebo & connection with ROS

"Exquisite store manager" youth entrepreneurship incubation camp - the first phase of Shunde market has been successfully completed!

让我们,从头到尾,通透网络I/O模型
随机推荐
THREE. AxesHelper is not a constructor
身体质量指数程序,入门写死的小程序项目
Gazebo的安装&与ROS的连接
Metauniverse urban legend 02: metaphor of the number one player
Informatics Orsay Ibn YBT 1172: find the factorial of n within 10000 | 1.6 14: find the factorial of n within 10000
C # method of calculating lunar calendar date 2022
golang中的atomic,以及CAS操作
What are the differences between Oracle Linux and CentOS?
Transplant DAC chip mcp4725 to nuc980
Installation of torch and torch vision in pytorch
Data type of pytorch tensor
Table table setting fillet
负载均衡性能参数如何测评?
分享一个通用的so动态库的编译方法
让我们,从头到尾,通透网络I/O模型
736. Lisp 语法解析 : DFS 模拟题
安利一波C2工具
Oracle: Practice of CDB restricting PDB resources
c语言—数组
The cost of returning tables in MySQL