当前位置:网站首页>每日一题: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;
}
};边栏推荐
- js4day(DOM开始:获取DOM元素内容,修改元素样式,修改表单元素属性,setInterval定时器,轮播图案例)
- 中文姓名提取(玩具代码——准头太小,权当玩闹)
- How to get the operating system running PHP- How to get the OS on which PHP is running?
- Counting class DP acwing 900 Integer partition
- MySQL: Invalid GIS data provided to function st_ geometryfromtext
- spfa AcWing 852. SPFA judgement negative ring
- Js10day (API phased completion, regular expression introduction, custom attributes, filtering sensitive word cases, registration module verification cases)
- Unity skframework framework (XVI), package manager development kit Manager
- How can attribute mapping of entity classes be without it?
- Unity SKFramework框架(二十一)、Texture Filter 贴图资源筛选工具
猜你喜欢

移动式布局(流式布局)

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

Unity skframework framework (XIX), POI points of interest / information points

JS iterator generator asynchronous code processing promise+ generator - > await/async

Js4day (DOM start: get DOM element content, modify element style, modify form element attributes, setinterval timer, carousel Map Case)

Counting class DP acwing 900 Integer partition

Ali was killed by two programming problems at the beginning, pushed inward again, and finally landed (he has taken an electronic offer)

spfa AcWing 851. SPFA finding the shortest path

Unity skframework framework (XV), singleton singleton

Hash table acwing 840 Simulated hash table
随机推荐
Dijkstra AcWing 850. Dijkstra finding the shortest circuit II
Unity skframework framework (XVIII), roamcameracontroller roaming perspective camera control script
[200 opencv routines] 100 Adaptive local noise reduction filter
腾讯三面:进程写文件过程中,进程崩溃了,文件数据会丢吗?
(7) Web security | penetration testing | how does network security determine whether CND exists, and how to bypass CND to find the real IP
Heap acwing 838 Heap sort
[opencv learning] [image pyramid]
Unity SKFramework框架(十八)、RoamCameraController 漫游视角相机控制脚本
Explain in detail the process of realizing Chinese text classification by CNN
js1day(輸入輸出語法,數據類型,數據類型轉換,var和let區別)
Crowncad (crown CAD), the first fully independent 3D CAD platform based on Cloud Architecture in China
de4000h存储安装配置
Everyone wants to eat a broken buffet. It's almost cold
PXE installation UOS prompt NFS over TCP not available from 10 x.x.x
Domestic free data warehouse ETL dispatching automation operation and maintenance expert taskctl
js2day(又是i++和++i,if语句,三元运算符,switch、while语句,for循环语句)
[error record] cannot open "XXX" because Apple cannot check whether it contains malware
West digital decided to raise the price of flash memory products immediately after the factory was polluted by materials
Get started REPORT | today, talk about the microservice architecture currently used by Tencent
[opencv learning] [image filtering]