当前位置:网站首页>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
边栏推荐
- Add the applet "lazycodeloading": "requiredcomponents" in taro,
- 如何管理分布式团队?
- Grc: personal information protection law, personal privacy, corporate risk compliance governance
- AI automatically generates annotation documents from code
- Tensorflow 1.14 specify GPU running settings
- Dynamic planning idea "from getting started to giving up"
- Go zero micro service practical series (IX. ultimate optimization of seckill performance)
- 736. Lisp 语法解析 : DFS 模拟题
- Match VIM from zero (0) -- Introduction to vimscript
- Dark horse notes - exception handling
猜你喜欢
405 method not allowed appears when the third party jumps to the website
云呐-工单管理制度及流程,工单管理规范
1123. The nearest common ancestor of the deepest leaf node
云呐|工单管理软件,工单管理软件APP
【C语言进阶篇】指针的8道笔试题
Can the system hibernation file be deleted? How to delete the system hibernation file
Data type of pytorch tensor
Tensorflow GPU installation
"Exquisite store manager" youth entrepreneurship incubation camp - the first phase of Shunde market has been successfully completed!
go-zero微服务实战系列(九、极致优化秒杀性能)
随机推荐
ARM裸板调试之JTAG调试体验
Atomic in golang and CAS operations
Boot - Prometheus push gateway use
Tensorflow GPU installation
自旋与sleep的区别
Supersocket 1.6 creates a simple socket server with message length in the header
阿里云中mysql数据库被攻击了,最终数据找回来了
Taro applet enables wxml code compression
系统休眠文件可以删除吗 系统休眠文件怎么删除
Spark TPCDS Data Gen
斗地主游戏的案例开发
Data type of pytorch tensor
[signal and system]
云呐|工单管理办法,如何开展工单管理
Js逆向——捅了【马蜂窝】的ob混淆与加速乐
Gnet: notes on the use of a lightweight and high-performance go network framework
实现mysql与ES的增量数据同步
How to manage distributed teams?
[case sharing] basic function configuration of network loop detection
Send template message via wechat official account