当前位置:网站首页>【日常训练】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));
}
}
边栏推荐
- Redis efficient like and cancel function
- robots. Txt restrict search engine inclusion
- Xception learning notes
- Codeforces Round #416 (Div. 2) C. Vladik and Memorable Trip
- MySQL knowledge points
- Big orange crazy blog move notice
- Gartner research: in China, the adoption of hybrid cloud has become the mainstream trend
- Lavaweb [first understanding the solution of subsequent problems]
- 几行事务代码,让我赔了16万
- Introduction and basic knowledge of machine learning
猜你喜欢
Lavaweb [first understanding the solution of subsequent problems]
[QT] add knowledge supplement of third-party database
如何校验两个文件内容是否相同
Redis高效点赞与取消功能
Od modify DLL and exe pop-up contents [OllyDbg]
Mysql知识点
Redis 教程
Elk elegant management server log
【机器学习】向量化计算 -- 机器学习路上必经路
Huawei operator level router configuration example | BGP VPLS and LDP VPLS interworking example
随机推荐
SSH configuration password free login error: /usr/bin/ssh copy ID: error: no identities found solution
STM32——一线协议之DS18B20温度采样
【微信小程序开发】样式汇总
IEDA 右键源码文件菜单简介
[applet project development -- JD mall] uni app commodity classification page (first)
Dart training and sphygmomanometer inflation pump power control DPC
Design practice of current limiting components
Druid监控统计数据源
Share Creators萌芽人才培养计划来了!
LeetCode_栈_困难_227.基本计算器(不含乘除)
The best learning method in the world: Feynman learning method
LeetCode_ Stack_ Difficulties_ 227. basic calculator (excluding multiplication and division)
If I am in Beijing, where is a better place to open an account? In addition, is it safe to open a mobile account?
[exsi] transfer files between hosts
Network address translation (NAT) technology
Gartner research: in China, the adoption of hybrid cloud has become the mainstream trend
鼠标悬停效果八
Chapitre 03 Bar _ Gestion des utilisateurs et des droits
Mouse over effect IV
kubernetes资源对象介绍及常用命令(二)