当前位置:网站首页>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;
}
};边栏推荐
- Unity skframework framework (XXI), texture filter map resource filtering tool
- Apply lnk306gn-tl converter, non isolated power supply
- The coloring method determines the bipartite graph acwing 860 Chromatic judgement bipartite graph
- ADB basic commands
- Linear DP acwing 902 Shortest editing distance
- 3 a VTT terminal regulator ncp51200mntxg data
- bellman-ford AcWing 853. Shortest path with side limit
- How can attribute mapping of entity classes be without it?
- mac(macos Monterey12.2 m1) 个人使用php开发
- JS iterator generator asynchronous code processing promise+ generator - > await/async
猜你喜欢

C operator

屠榜多目标跟踪!BoT-SORT:稳健的关联多行人跟踪

Unity SKFramework框架(十五)、Singleton 单例
![[opencv] [image gradient]](/img/37/1f366501e2b4e70ecee6365088167c.jpg)
[opencv] [image gradient]
![[opencv learning] [template matching]](/img/4c/7214329a34974c59b4931c08046ee8.jpg)
[opencv learning] [template matching]

Unity skframework framework (XV), singleton singleton

移动式布局(流式布局)

Linear DP acwing 897 Longest common subsequence

Interesting interview questions

Unity skframework framework (XVI), package manager development kit Manager
随机推荐
Linear DP acwing 902 Shortest editing distance
Linear DP acwing 899 Edit distance
面渣逆袭:MySQL六十六问,两万字+五十图详解!有点六
自主可控三维云CAD:CrownCAD赋能企业创新设计
Unity skframework framework (XVIII), roamcameracontroller roaming perspective camera control script
Std:: vector batch import fast de duplication method
中文姓名提取(玩具代码——准头太小,权当玩闹)
Get started REPORT | today, talk about the microservice architecture currently used by Tencent
上海交大教授:何援军——包围盒(包容体/包围盒子)
Finally, someone explained the supervised learning clearly
Js4day (DOM start: get DOM element content, modify element style, modify form element attributes, setinterval timer, carousel Map Case)
Heap acwing 839 Simulated reactor
Async/await asynchronous function
移动式布局(流式布局)
Jerry's watch modifies the alarm clock [chapter]
To bypass obregistercallbacks, you need to drive the signature method
How can attribute mapping of entity classes be without it?
Jerry's watch reads the alarm clock [chapter]
Unity skframework framework (XIV), extension extension function
spfa AcWing 852. SPFA judgement negative ring