当前位置:网站首页>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
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
边栏推荐
- JSP and filter
- Server coding bug
- R语言使用data函数获取当前R环境可用的示例数据集:获取datasets包中的所有示例数据集、获取所有包的数据集、获取特定包的数据集
- R language uses the data function to obtain the sample datasets available in the current R environment: obtain all the sample datasets in the datasets package, obtain the datasets of all packages, and
- Detailed explanation of multithreading
- STM32 and motor development (from MCU to architecture design)
- MySQL
- Oracle memory management
- JSON serialization case summary
- php 迷宫游戏
猜你喜欢

mysql更新时条件为一查询

Annotation and reflection

AI 考高数得分 81,网友:AI 模型也免不了“内卷”!

rxjs Observable filter Operator 的实现原理介绍

Tutoriel PowerPoint, comment enregistrer une présentation sous forme de vidéo dans Powerpoint?

Flink SQL knows why (XV): changed the source code and realized a batch lookup join (with source code attached)

Flink SQL knows why (XI): weight removal is not only count distinct, but also powerful duplication

SQL Injection (GET/Select)

18W word Flink SQL God Road manual, born in the sky

Flink SQL knows why (17): Zeppelin, a sharp tool for developing Flink SQL
随机推荐
Flink SQL knows why (VIII): the wonderful way to parse Flink SQL tumble window
IBEM mathematical formula detection data set
今日睡眠质量记录77分
SQL Injection (POST/Search)
CVPR 2022 | 美团技术团队精选6篇优秀论文解读
Convolution emotion analysis task4
ThreadPoolExecutor realizes multi-threaded concurrency and obtains the return value (elegant and concise way)
Annotation and reflection
Useful blog links
Box layout of Kivy tutorial BoxLayout arranges sub items in vertical or horizontal boxes (tutorial includes source code)
JS convert pseudo array to array
Heap structure and heap sort heapify
PowerPoint 教程,如何在 PowerPoint 中將演示文稿另存為視頻?
[how to solve FAT32 when the computer is inserted into the U disk or the memory card display cannot be formatted]
父亲和篮球
HALCON联合C#检测表面缺陷——HALCON例程autobahn
已解决TypeError: Argument ‘parser‘ has incorrect type (expected lxml.etree._BaseParser, got type)
Introduction to the implementation principle of rxjs observable filter operator
Disruptor -- a high concurrency and high performance queue framework for processing tens of millions of levels
JS 将伪数组转换成数组