当前位置:网站首页>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

边栏推荐
猜你喜欢

2022 Google CTF SEGFAULT LABYRINTH wp

ESP Arduino (IV) PWM waveform control output

Go zero micro service practical series (IX. ultimate optimization of seckill performance)

身体质量指数程序,入门写死的小程序项目

云呐|工单管理软件,工单管理软件APP

Transformation transformation operator

LeetCode:1175. 质数排列

Transplant DAC chip mcp4725 to nuc980

第三方跳转网站 出现 405 Method Not Allowed
![[hfctf2020]babyupload session parsing engine](/img/db/6003129bc16f943ad9868561a2d5dc.png)
[hfctf2020]babyupload session parsing engine
随机推荐
字节P7专业级讲解:接口测试常用工具及测试方法,福利文
What are the differences between Oracle Linux and CentOS?
Informatics Olympiad YBT 1171: factors of large integers | 1.6 13: factors of large integers
"Exquisite store manager" youth entrepreneurship incubation camp - the first phase of Shunde market has been successfully completed!
Using the entry level of DVA in taro3.*
树莓派/arm设备上安装火狐Firefox浏览器
Tensorflow GPU installation
系统休眠文件可以删除吗 系统休眠文件怎么删除
负载均衡性能参数如何测评?
Send template message via wechat official account
Openjudge noi 1.7 10: simple password
taro3.*中使用 dva 入门级别的哦
Meet in the middle
剑指 Offer II 035. 最小时间差-快速排序加数据转换
Add the applet "lazycodeloading": "requiredcomponents" in taro,
交叉验证如何防止过拟合
黑马笔记---创建不可变集合与Stream流
Force buckle 1037 Effective boomerang
MySQL中回表的代价
Informatics Orsay Ibn YBT 1172: find the factorial of n within 10000 | 1.6 14: find the factorial of n within 10000