当前位置:网站首页>每日一题:1175.质数排列
每日一题:1175.质数排列
2022-07-02 09:58:00 【Base-Case】
请你帮忙给从 1 到 n 的数设计排列方案,使得所有的「质数」都应该被放在「质数索引」(索引从 1 开始)上;你需要返回可能的方案总数。
让我们一起来回顾一下「质数」:质数一定是大于 1 的,并且不能用两个小于它的正整数的乘积来表示。
由于答案可能会很大,所以请你返回答案 模 mod 10^9 + 7 之后的结果即可。
示例 1:
输入:n = 5
输出:12
解释:举个例子,[1,2,5,4,3] 是一个有效的排列,但 [5,2,3,4,1] 不是,因为在第二种情况里质数 5 被错误地放在索引为 1 的位置上。
示例 2:
输入:n = 100
输出:682289015
提示:
1 <= n <= 100
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/prime-arrangements
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
class Solution {
public:
bool vt[110];
int primes[110];
int numPrimeArrangements(int n) {
int mod=1e9+7;
int cnt=0;
//筛质数
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;
//质数全排列
for(int i=2;i<=cnt;i++){
sum=(sum*i)%mod;
}
//素数全排列
for(int i=n-cnt;i>1;i--){
sum=(sum*i)%mod;
}
return sum;
}
};
边栏推荐
- 三面阿里,有惊无险成功拿到offer定级P7,只能说是真的难
- To bypass obregistercallbacks, you need to drive the signature method
- Unity SKFramework框架(十四)、Extension 扩展函数
- Js5day (event monitoring, function assignment to variables, callback function, environment object this, select all, invert selection cases, tab column cases)
- Unity skframework framework (XIV), extension extension function
- spfa AcWing 852. SPFA judgement negative ring
- 阿里发布的Redis开发文档,涵盖了所有的redis操作
- Counting class DP acwing 900 Integer partition
- C modifier
- js3day(数组操作,js冒泡排序,函数,调试窗口,作用域及作用域链,匿名函数,对象,Math对象)
猜你喜欢
Unity skframework framework (XIV), extension extension function
Unity SKFramework框架(二十)、VFX Lab 特效库
[200 opencv routines] 100 Adaptive local noise reduction filter
上海交大教授:何援军——包围盒(包容体/包围盒子)
难忘阿里,4面技术5面HR附加笔试面,走的真艰难真心酸
Day4 operator, self increasing, self decreasing, logical operator, bit operation, binary conversion decimal, ternary operator, package mechanism, document comment
【蓝桥杯选拔赛真题43】Scratch航天飞行 少儿编程scratch蓝桥杯选拔赛真题讲解
Unity skframework framework (XV), singleton singleton
Unity skframework Framework (XVI), package manager Development Kit Manager
Interval DP acwing 282 Stone merging
随机推荐
3 a VTT terminal regulator ncp51200mntxg data
屠榜多目标跟踪!BoT-SORT:稳健的关联多行人跟踪
Hash table acwing 840 Simulated hash table
C modifier
国内首款、完全自主、基于云架构的三维CAD平台——CrownCAD(皇冠CAD)
Fundamentals of face recognition (facenet)
无向图的桥
[opencv learning] [image filtering]
Unity SKFramework框架(十九)、POI 兴趣点/信息点
TVOC, VOC, VOCs gas detection + Solution
bellman-ford AcWing 853. Shortest path with side limit
(6) Web security | penetration test | network security encryption and decryption ciphertext related features, with super encryption and decryption software
Crowncad (crown CAD), the first fully independent 3D CAD platform based on Cloud Architecture in China
Linear DP acwing 902 Shortest editing distance
日本赌国运:Web3.0 ,反正也不是第一次失败了!
net share
JS generates 4-digit verification code
JSON serialization and parsing
Jerry's weather code table [chapter]
【云原生数据库】遇到慢SQL该怎么办(上)?