当前位置:网站首页>Daily question: 1175 Prime permutation
Daily question: 1175 Prime permutation
2022-07-02 13:13:00 【Base-Case】
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
source : Power button (LeetCode)
link :https://leetcode.cn/problems/prime-arrangements
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
class Solution {
public:
bool vt[110];
int primes[110];
int numPrimeArrangements(int n) {
int mod=1e9+7;
int cnt=0;
// Sieve prime number
for(int i=2;i<=n;i++){
if(!vt[i]) primes[cnt++]=i;
for(int j=0;primes[j]<=n/i;j++){
vt[i*primes[j]]=true;
if(i%primes[j]==0) break;
}
}
long long sum=1;
// Prime total permutation
for(int i=2;i<=cnt;i++){
sum=(sum*i)%mod;
}
// Primes are all arranged
for(int i=n-cnt;i>1;i--){
sum=(sum*i)%mod;
}
return sum;
}
};边栏推荐
- Linear DP acwing 895 Longest ascending subsequence
- [opencv learning] [image histogram and equalization]
- [200 opencv routines] 100 Adaptive local noise reduction filter
- Finally, someone explained the supervised learning clearly
- How can attribute mapping of entity classes be without it?
- js3day(数组操作,js冒泡排序,函数,调试窗口,作用域及作用域链,匿名函数,对象,Math对象)
- 8A Synchronous Step-Down regulator tps568230rjer_ Specification information
- TVOC, VOC, VOCs gas detection + Solution
- Jerry's watch ringtone audition [article]
- Unity SKFramework框架(十七)、FreeCameraController 上帝视角/自由视角相机控制脚本
猜你喜欢
![[opencv learning] [common image convolution kernel]](/img/15/d1e8b8aa3c613755e64edb8c9a0f54.jpg)
[opencv learning] [common image convolution kernel]

Heap acwing 839 Simulated reactor
![[opencv learning] [Canny edge detection]](/img/8b/37694ae2f0f13f829f3c033da0605e.jpg)
[opencv learning] [Canny edge detection]
![Jerry's watch delete alarm clock [chapter]](/img/7f/d51b37872b4ce905a0a723a514b2dc.jpg)
Jerry's watch delete alarm clock [chapter]

Linear DP acwing 896 Longest ascending subsequence II

Async/await asynchronous function

SAP MM 因物料有负库存导致MMPV开账期失败问题之对策

Unforgettable Ali, 4 skills, 5 hr additional written tests, it's really difficult and sad to walk

Unity skframework framework (XVII), freecameracontroller God view / free view camera control script

Ali was killed by two programming problems at the beginning, pushed inward again, and finally landed (he has taken an electronic offer)
随机推荐
最近公共祖先LCA的三种求法
Heap acwing 839 Simulated reactor
Rust language document Lite (Part 1) - cargo, output, basic syntax, data type, ownership, structure, enumeration and pattern matching
Linear DP acwing 895 Longest ascending subsequence
Ntmfs4c05nt1g N-ch 30V 11.9a MOS tube, pdf
Lucky numbers in the [leetcode daily question] matrix
SAP MM 因物料有负库存导致MMPV开账期失败问题之对策
Js5day (event monitoring, function assignment to variables, callback function, environment object this, select all, invert selection cases, tab column cases)
Tencent three sides: in the process of writing files, the process crashes, and will the file data be lost?
js4day(DOM开始:获取DOM元素内容,修改元素样式,修改表单元素属性,setInterval定时器,轮播图案例)
挥发性有机物TVOC、VOC、VOCS气体检测+解决方案
自主可控三维云CAD:CrownCAD赋能企业创新设计
spfa AcWing 852. SPFA judgement negative ring
Jerry's watch time synchronization [chapter]
三面阿里,有惊无险成功拿到offer定级P7,只能说是真的难
阿里初面被两道编程题给干掉,再次内推终上岸(已拿电子offer)
C modifier
腾讯三面:进程写文件过程中,进程崩溃了,文件数据会丢吗?
[opencv learning] [image histogram and equalization]
How to get the operating system running PHP- How to get the OS on which PHP is running?