当前位置:网站首页>【日常训练】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));
}
}
边栏推荐
- Mouse over effect I
- Summary of problems encountered in debugging positioning and navigation
- js中的原型和原型链
- The operation efficiency of the park is improved, and the application platform management of applet container technology is accelerated
- XXL job User Guide
- 鼠标悬停效果六
- UE4 rendering pipeline learning notes
- xxl-job使用指南
- 第03章_用戶與權限管理
- VMware vSphere 6.7 virtualization cloud management 12. Vcsa6.7 update vCenter server license
猜你喜欢

Elk elegant management server log

UE4 rendering pipeline learning notes

Design practice of current limiting components

STM32 - DS18B20 temperature sampling of first-line protocol

Sampling Area Lights

servlet【初识】

So easy deploy program to server
![[machine learning] vectorized computing -- a must on the way of machine learning](/img/3f/d672bb254f845ea705b3a0ca10ee19.png)
[machine learning] vectorized computing -- a must on the way of machine learning

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

PHP batch Excel to word
随机推荐
SSH configuration password free login error: /usr/bin/ssh copy ID: error: no identities found solution
Codeforces Round #416 (Div. 2) C. Vladik and Memorable Trip
[wechat applet development] style summary
Ipmitool download address and possible problems during compilation and installation
Lavaweb [first understanding the solution of subsequent problems]
A shooting training method based on the digital measurement of Joule energy and posture of sphygmomanometer air bag with standard air pressure
Summary of problems encountered in debugging positioning and navigation
单片机 MCU 固件打包脚本软件
js 找出两个数组中的重复元素
Optimal transport Series 1
Densenet network paper learning notes
xxl-job使用指南
彻底解决Lost connection to MySQL server at ‘reading initial communication packet
[exsi] transfer files between hosts
【小程序项目开发--京东商城】uni-app之自定义搜索组件(上)
第03章_用戶與權限管理
MySQL index --01--- design principle of index
Druid monitoring statistics source
[linear DP] longest common subsequence
【Qt】添加第三方库的知识补充