当前位置:网站首页>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;
}
};
边栏推荐
- Get started REPORT | today, talk about the microservice architecture currently used by Tencent
- Unity skframework framework (XVIII), roamcameracontroller roaming perspective camera control script
- 挥发性有机物TVOC、VOC、VOCS气体检测+解决方案
- Finally, someone explained the supervised learning clearly
- Tencent three sides: in the process of writing files, the process crashes, and will the file data be lost?
- Structured data, semi-structured data and unstructured data
- [opencv learning] [moving object detection]
- Counting class DP acwing 900 Integer partition
- leetcode621. 任务调度器
- Js4day (DOM start: get DOM element content, modify element style, modify form element attributes, setinterval timer, carousel Map Case)
猜你喜欢
Js5day (event monitoring, function assignment to variables, callback function, environment object this, select all, invert selection cases, tab column cases)
三翼鸟两周年:羽翼渐丰,腾飞指日可待
Word efficiency guide - word's own template
OpenApi-Generator:简化RESTful API开发流程
Tencent three sides: in the process of writing files, the process crashes, and will the file data be lost?
Five best software architecture patterns that architects must understand
[opencv learning] [template matching]
[200 opencv routines] 100 Adaptive local noise reduction filter
Everyone wants to eat a broken buffet. It's almost cold
Jerry's watch stops ringing [article]
随机推荐
[opencv learning] [contour detection]
架构师必须了解的 5 种最佳软件架构模式
Redis数据库持久化
JS逆向之巨量创意signature签名
[opencv learning] [template matching]
Package management tools
Linear DP acwing 895 Longest ascending subsequence
解答:EasyDSS视频点播时音频是否可以设置为默认开启?
Heap acwing 839 Simulated reactor
三翼鸟两周年:羽翼渐丰,腾飞指日可待
Variable, "+" sign, data type
[opencv learning] [moving object detection]
To bypass obregistercallbacks, you need to drive the signature method
bellman-ford AcWing 853. Shortest path with side limit
How can attribute mapping of entity classes be without it?
(6) Web security | penetration test | network security encryption and decryption ciphertext related features, with super encryption and decryption software
Lucky numbers in the [leetcode daily question] matrix
linux下清理系统缓存并释放内存
Unity skframework framework (XIV), extension extension function
【OpenGL】笔记二十九、高级光照(镜面高光)