当前位置:网站首页>【日常训练】1175. 质数排列
【日常训练】1175. 质数排列
2022-07-01 02:57:00 【Puppet__】
题目
请你帮忙给从 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
代码
package dayLeetCode;
public class dayleetcode1175 {
int mod = 1000000007;
// 求有多少个质数,然后将质数和合数全排列即可
public int numPrimeArrangements(int n) {
int numPrimes = 0;
for (int i = 1; i <= n; i++){
if (isPrime(i)){
numPrimes++;
}
}
return (int)(f(numPrimes) % mod* f(n - numPrimes) % mod) % mod;
}
// 判断是否是质数
private boolean isPrime(int k) {
if (k == 1){
return false;
}
for (int i = 2; i * i <= k; i++){
if (k % i == 0){
return false;
}
}
return true;
}
// 求阶乘
long f(int k){
long ans = 1;
for (int i = 1; i <= k; i++){
ans *= i;
ans %= mod;
}
return ans;
}
public static void main(String[] args) {
dayleetcode1175 obj = new dayleetcode1175();
System.out.println(obj.numPrimeArrangements(100));
}
}
边栏推荐
- 鼠标悬停效果九
- How to verify whether the contents of two files are the same
- Huawei operator level router configuration example | configuration optionA mode cross domain LDP VPLS example
- Chapitre 03 Bar _ Gestion des utilisateurs et des droits
- 单片机 MCU 固件打包脚本软件
- 实战 ELK 优雅管理服务器日志
- Why are strings immutable in many programming languages? [repeated] - why are strings immutable in many programming languages? [duplicate]
- Share Creators萌芽人才培養計劃來了!
- 【机器学习】向量化计算 -- 机器学习路上必经路
- 鼠标悬停效果三
猜你喜欢
![[QT] add knowledge supplement of third-party database](/img/ea/ca8b07ad80485208f2bb8ee8a78a28.png)
[QT] add knowledge supplement of third-party database

# 使用 KubeKey 搭建 Kubernetes/KubeSphere 环境的'心路(累)历程'

【小程序项目开发-- 京东商城】uni-app之分类导航区域

js中的原型和原型链

PCB defect detection based on OpenCV and image subtraction

Gartner research: in China, the adoption of hybrid cloud has become the mainstream trend

记一次服务部署失败问题排查

Huawei operator level router configuration example | configuration static VPLS example

【小程序项目开发--京东商城】uni-app之自定义搜索组件(上)

Mnasnet learning notes
随机推荐
ssh配置免密登录时报错:/usr/bin/ssh-copy-id: ERROR: No identities found 解决方法
LeetCode_栈_困难_227.基本计算器(不含乘除)
产业互联网中,「小」程序有「大」作为
[exsi] transfer files between hosts
servlet【初识】
Druid monitoring statistics source
xxl-job使用指南
鼠标悬停效果十
鼠标悬停效果九
Redis分布式锁的8大坑
最新接口自动化面试题
How to determine the progress bar loaded in the loading interface when opening the game
[applet project development -- Jingdong Mall] user defined search component of uni app (Part 1)
Redis efficient like and cancel function
php批量excel转word
Restcloud ETL WebService data synchronization to local
Druid监控统计数据源
调试定位导航遇到的问题总结
Mouse over effect 9
【EXSI】主机间传输文件