当前位置:网站首页>【日常训练】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));
}
}
边栏推荐
- A shooting training method based on the digital measurement of Joule energy and posture of sphygmomanometer air bag with standard air pressure
- Huawei operator level router configuration example | BGP VPLS configuration example
- LeetCode_栈_困难_227.基本计算器(不含乘除)
- Youmeng (a good helper for real-time monitoring of software exceptions: crash) access tutorial (the easiest tutorial for Xiaobai with some foundation)
- js中的原型和原型链
- Classic programming problem: finding the number of daffodils
- 【微信小程序开发】样式汇总
- Huawei operator level router configuration example | configuration optionA mode cross domain LDP VPLS example
- 第03章_用户与权限管理
- [exsi] transfer files between hosts
猜你喜欢

The best learning method in the world: Feynman learning method

园区运营效率提升,小程序容器技术加速应用平台化管理

Const and the secret of pointers

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

Network address translation (NAT) technology

Dell server restart Idrac method

Detailed explanation of pointer array and array pointer (comprehensive knowledge points)

MySQL knowledge points

安装VCenter6.7【VCSA6.7(vCenter Server Appliance 6.7) 】

记一次服务部署失败问题排查
随机推荐
Metadata in NFT
Youmeng (a good helper for real-time monitoring of software exceptions: crash) access tutorial (the easiest tutorial for Xiaobai with some foundation)
产业互联网中,「小」程序有「大」作为
【微信小程序开发】样式汇总
[applet project development -- Jingdong Mall] user defined search component of uni app (Part 1)
鼠标悬停效果一
Poj-3486-computers[dynamic planning]
[applet project development -- JD mall] uni app commodity classification page (Part 2)
Codeforces Round #416 (Div. 2) C. Vladik and Memorable Trip
鼠标悬停效果十
Huawei operator level router configuration example | configuration optionA mode cross domain LDP VPLS example
Borrowing constructor inheritance and composite inheritance
A shooting training method based on the digital measurement of Joule energy and posture of sphygmomanometer air bag with standard air pressure
Cloud native annual technology inventory is released! Ride the wind and waves at the right time
Catch 222222
Mouse over effect 8
Chapitre 03 Bar _ Gestion des utilisateurs et des droits
Sampling Area Lights
Redis高效点赞与取消功能
单片机 MCU 固件打包脚本软件