当前位置:网站首页>Leetcode-1175.Prime Arrangements
Leetcode-1175.Prime Arrangements
2022-07-03 13:11:00 【Eistert】
题目
Return the number of permutations of 1 to n so that prime numbers are at prime indices (1-indexed.)
(Recall that an integer is prime if and only if it is greater than 1, and cannot be written as a product of two positive integers both smaller than it.)
Since the answer may be large, return the answer modulo 109 + 7.
permutations [数]排列(permutation 的复数)
modulo 模数
解法
方法一:质数判断+组合数学
求复合条件的方案数,使得所有质数都放在质数索引上,所有合数放在合数索引上,质数放置和合数放置是相互独立的,总的方案数即为【所有质数都放在质数索引上的方案数】*【所有合数都放在合数索引上的方案数】。求【所有质数都放在质数索引上的方案数】,即求质数个数numPrimes的阶乘。【所有合数都放在合数索引上的方案数】同理。求质数个数时,可以使用试除法。【204.计数质数的官方题解】列举了更多的求质数个数的方法,最后注意计算过程中需要对109+7取模。
代码
package com.leetcode.question.medium;
/** * 1175 Prime Arrangements * * @ClassName PrimeArrangements1175 * @Author eistert * @Date 2022/7/1 18:01 **/
public class PrimeArrangements1175 {
public static void main(String[] args) {
}
static final int MOD = 1000000007;
public int numPrimeArrangements(int n) {
int numPrimes = 0;
for (int i = 1; i <= n; i++) {
if (isPrime(i)) {
numPrimes++;
}
}
// 为什么质数放置和合数放置是相互独立的,总的方案数即为
long total = factorial(numPrimes) * factorial(n - numPrimes);
long totalMod = total % MOD;
return (int) totalMod;
}
/** * 是否为质数 */
public boolean isPrime(int n) {
if (n == 1) {
return false;
}
for (int i = 2; i * i <= n; i++) {
// 为什么是i * i <= n
if (n % i == 0) {
return false;
}
}
return true;
}
/** * 阶乘 */
public long factorial(int n) {
long res = 1;
for (int i = 1; i <= n; i++) {
res *= i;
res %= MOD;
}
return res;
}
}
转载
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/prime-arrangements
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
边栏推荐
- User and group command exercises
- Shell timing script, starting from 0, CSV format data is regularly imported into PostgreSQL database shell script example
- Detailed explanation of multithreading
- SQL Injection (GET/Select)
- In the promotion season, how to reduce the preparation time of defense materials by 50% and adjust the mentality (personal experience summary)
- mysql更新时条件为一查询
- Stack application (balancer)
- (first) the most complete way to become God of Flink SQL in history (full text 180000 words, 138 cases, 42 pictures)
- Comprehensive evaluation of double chain notes remnote: fast input, PDF reading, interval repetition / memory
- 71 articles on Flink practice and principle analysis (necessary for interview)
猜你喜欢
SQL Injection (GET/Select)
【历史上的今天】7 月 3 日:人体工程学标准法案;消费电子领域先驱诞生;育碧发布 Uplay
KEIL5出现中文字体乱码的解决方法
Flink SQL knows why (12): is it difficult to join streams? (top)
The shortage of graphics cards finally came to an end: 3070ti for more than 4000 yuan, 2000 yuan cheaper than the original price, and 3090ti
Comprehensive evaluation of double chain notes remnote: fast input, PDF reading, interval repetition / memory
18W word Flink SQL God Road manual, born in the sky
人身变声器的原理
106. 如何提高 SAP UI5 应用路由 url 的可读性
106. How to improve the readability of SAP ui5 application routing URL
随机推荐
Flink SQL knows why (VIII): the wonderful way to parse Flink SQL tumble window
JSON serialization case summary
Spark practice 1: build spark operation environment in single node local mode
logback日志的整理
开始报名丨CCF C³[email protected]奇安信:透视俄乌网络战 —— 网络空间基础设施面临的安全对抗与制裁博弈...
R语言gt包和gtExtras包优雅地、漂亮地显示表格数据:nflreadr包以及gtExtras包的gt_plt_winloss函数可视化多个分组的输赢值以及内联图(inline plot)
[how to solve FAT32 when the computer is inserted into the U disk or the memory card display cannot be formatted]
Flink SQL knows why (13): is it difficult to join streams? (next)
71 articles on Flink practice and principle analysis (necessary for interview)
Ubuntu 14.04 下开启PHP错误提示
CVPR 2022 | 美团技术团队精选6篇优秀论文解读
Useful blog links
AI scores 81 in high scores. Netizens: AI model can't avoid "internal examination"!
Task6: using transformer for emotion analysis
8皇后问题
rxjs Observable filter Operator 的实现原理介绍
Today's sleep quality record 77 points
Mysql database basic operation - regular expression
Setting up remote links to MySQL on Linux
使用tensorflow进行完整的DNN深度神经网络CNN训练完成图片识别案例