当前位置:网站首页>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
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
边栏推荐
- Setting up remote links to MySQL on Linux
- Bidirectional linked list (we only need to pay attention to insert and delete functions)
- IBEM 数学公式检测数据集
- pytorch 载入历史模型时更换gpu卡号,map_location设置
- Unity EmbeddedBrowser浏览器插件事件通讯
- 太阳底下无新事,元宇宙能否更上层楼?
- Flink SQL knows why (XI): weight removal is not only count distinct, but also powerful duplication
- HALCON联合C#检测表面缺陷——HALCON例程autobahn
- Libuv库 - 设计概述(中文版)
- Comprehensive evaluation of double chain notes remnote: fast input, PDF reading, interval repetition / memory
猜你喜欢

MySQL constraints

The latest BSC can pay dividends. Any B usdt Shib eth dividend destruction marketing can

8皇后问题

Today's sleep quality record 77 points

使用tensorflow进行完整的DNN深度神经网络CNN训练完成图片识别案例

TensorBoard可视化处理案例简析
![[how to solve FAT32 when the computer is inserted into the U disk or the memory card display cannot be formatted]](/img/95/09552d33d2a834af4d304129714775.png)
[how to solve FAT32 when the computer is inserted into the U disk or the memory card display cannot be formatted]

用户和组命令练习

File uploading and email sending

Mycms we media mall v3.4.1 release, user manual update
随机推荐
Libuv Library - Design Overview (Chinese version)
双链笔记 RemNote 综合评测:快速输入、PDF 阅读、间隔重复/记忆
已解决TypeError: Argument ‘parser‘ has incorrect type (expected lxml.etree._BaseParser, got type)
Mycms we media mall v3.4.1 release, user manual update
Kivy教程之 如何通过字符串方式载入kv文件设计界面(教程含源码)
Students who do not understand the code can also send their own token, which is easy to learn BSC
Realize the recognition and training of CNN images, and process the cifar10 data set and other methods through the tensorflow framework
Unity render streaming communicates with unity through JS
JS convert pseudo array to array
Flink code is written like this. It's strange that the window can be triggered (bad programming habits)
Logseq evaluation: advantages, disadvantages, evaluation, learning tutorial
DQL basic query
R语言gt包和gtExtras包优雅地、漂亮地显示表格数据:nflreadr包以及gtExtras包的gt_plt_winloss函数可视化多个分组的输赢值以及内联图(inline plot)
显卡缺货终于到头了:4000多块可得3070Ti,比原价便宜2000块拿下3090Ti
(first) the most complete way to become God of Flink SQL in history (full text 180000 words, 138 cases, 42 pictures)
Anan's doubts
使用Tensorflow进行完整的深度神经网络CNN训练完成图片识别案例2
今日睡眠质量记录77分
Sequence table (implemented in C language)
Convolution emotion analysis task4