当前位置:网站首页>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
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
边栏推荐
- SQL Injection (POST/Search)
- STM32 and motor development (from MCU to architecture design)
- Mycms we media mall v3.4.1 release, user manual update
- pytorch 载入历史模型时更换gpu卡号,map_location设置
- Introduction to the implementation principle of rxjs observable filter operator
- 双链笔记 RemNote 综合评测:快速输入、PDF 阅读、间隔重复/记忆
- Bidirectional linked list (we only need to pay attention to insert and delete functions)
- 用户和组命令练习
- Flink SQL knows why (16): dlink, a powerful tool for developing enterprises with Flink SQL
- Box layout of Kivy tutorial BoxLayout arranges sub items in vertical or horizontal boxes (tutorial includes source code)
猜你喜欢
mysql更新时条件为一查询
[how to solve FAT32 when the computer is inserted into the U disk or the memory card display cannot be formatted]
研发团队资源成本优化实践
[redis] cache warm-up, cache avalanche and cache breakdown
Smbms project
The latest BSC can pay dividends. Any B usdt Shib eth dividend destruction marketing can
人身变声器的原理
今日睡眠质量记录77分
Flink SQL knows why (13): is it difficult to join streams? (next)
Several common optimization methods matlab principle and depth analysis
随机推荐
JS convert pseudo array to array
logback日志的整理
Static linked list (subscript of array instead of pointer)
SwiftUI 开发经验之作为一名程序员需要掌握的五个最有力的原则
Useful blog links
阿南的疑惑
Unity render streaming communicates with unity through JS
SVN添加文件时的错误处理:…\conf\svnserve.conf:12: Option expected
【被动收入如何挣个一百万】
Mobile phones and computers can be used, whole people, spoof code connections, "won't you Baidu for a while" teach you to use Baidu
When updating mysql, the condition is a query
MySQL
SQL Injection (POST/Search)
(first) the most complete way to become God of Flink SQL in history (full text 180000 words, 138 cases, 42 pictures)
物联网毕设 --(STM32f407连接云平台检测数据)
Will Huawei be the next one to fall
用户和组命令练习
Heap structure and heap sort heapify
MapReduce implements matrix multiplication - implementation code
Kivy tutorial how to automatically load kV files