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

边栏推荐
- NEON优化:性能优化经验总结
- How to prevent overfitting in cross validation
- Taro中添加小程序 “lazyCodeLoading“: “requiredComponents“,
- C语言实例_5
- Google发布安全更新,修复Chrome中已被利用的0 day
- Transformation transformation operator
- Machine learning: the difference between random gradient descent (SGD) and gradient descent (GD) and code implementation.
- [JS] obtain the N days before and after the current time or the n months before and after the current time (hour, minute, second, year, month, day)
- Openjudge noi 1.7 08: character substitution
- NEON优化:性能优化常见问题QA
猜你喜欢

阿里云中mysql数据库被攻击了,最终数据找回来了

1123. The nearest common ancestor of the deepest leaf node

MySQL script batch queries all tables containing specified field types in the database

HMM notes

Do you understand this patch of the interface control devaxpress WinForms skin editor?

从底层结构开始学习FPGA----FIFO IP的定制与测试

ARM裸板调试之JTAG原理

Analysis of mutex principle in golang

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

405 method not allowed appears when the third party jumps to the website
随机推荐
Atomic in golang and CAS operations
Lldp compatible CDP function configuration
[signal and system]
Metauniverse urban legend 02: metaphor of the number one player
Body mass index program, entry to write dead applet project
Machine learning: the difference between random gradient descent (SGD) and gradient descent (GD) and code implementation.
[case sharing] basic function configuration of network loop detection
MySQL script batch queries all tables containing specified field types in the database
NEON优化:矩阵转置的指令优化案例
736. Lisp 语法解析 : DFS 模拟题
Table table setting fillet
Neon Optimization: an optimization case of log10 function
Informatics Olympiad YBT 1171: factors of large integers | 1.6 13: factors of large integers
Docker method to install MySQL
NEON优化:性能优化经验总结
Install Firefox browser on raspberry pie /arm device
Can the system hibernation file be deleted? How to delete the system hibernation file
Make Jar, Not War
[JS] obtain the N days before and after the current time or the n months before and after the current time (hour, minute, second, year, month, day)
Oracle:CDB限制PDB资源实战