当前位置:网站首页>【日常训练】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));
}
}
边栏推荐
- LeetCode_栈_困难_227.基本计算器(不含乘除)
- PTA 1016
- 彻底解决Lost connection to MySQL server at ‘reading initial communication packet
- [applet project development -- Jingdong Mall] user defined search component of uni app (Part 1)
- 鼠标悬停效果三
- Share Creators萌芽人才培養計劃來了!
- Mouse over effect III
- Restcloud ETL data realizes incremental data synchronization through timestamp
- IEDA 右键源码文件菜单简介
- [applet project development -- JD mall] uni app commodity classification page (first)
猜你喜欢

servlet【初识】

Restcloud ETL practice data row column conversion

Restcloud ETL WebService data synchronization to local

Youmeng (a good helper for real-time monitoring of software exceptions: crash) access tutorial (the easiest tutorial for Xiaobai with some foundation)

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

POI导出excel,按照父子节点进行分级显示
![[applet project development -- Jingdong Mall] user defined search component of uni app (Part 1)](/img/73/a22ab1dbb46e743ffd5f78b40e66a2.png)
[applet project development -- Jingdong Mall] user defined search component of uni app (Part 1)
![Lavaweb [first understanding the solution of subsequent problems]](/img/8a/08cb2736c2c198d926dbe00c004c3f.png)
Lavaweb [first understanding the solution of subsequent problems]

Druid monitoring statistics source

【小程序项目开发 -- 京东商城】uni-app 商品分类页面(下)
随机推荐
UE4 rendering pipeline learning notes
Detailed explanation of pointer array and array pointer (comprehensive knowledge points)
鼠标悬停效果一
Mouse over effect II
Restcloud ETL practice data row column conversion
Mouse over effect III
Catch 222222
咱就是说 随便整几千个表情包为我所用一下
PCB defect detection based on OpenCV and image subtraction
How the network is connected: Chapter 2 (Part 2) packet receiving and sending operations between IP and Ethernet
Mouse over effect IV
【微信小程序开发】样式汇总
How to open a stock account? Also, is it safe to open an account online?
鼠标悬停效果七
Introduction and basic knowledge of machine learning
【小程序项目开发-- 京东商城】uni-app之首页商品楼层
如果在小券商办理网上开户安全吗?我的资金会不会不安全?
【小程序项目开发--京东商城】uni-app之自定义搜索组件(上)
彻底解决Lost connection to MySQL server at ‘reading initial communication packet
Densenet network paper learning notes