当前位置:网站首页>Leetcode-1175. Prime Arrangements
Leetcode-1175. Prime Arrangements
2022-07-03 13:47:00 【Eistert】
subject
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 [ Count ] array (permutation Complex number )
modulo modulus
solution
Method 1 : Prime number judgment + Combinatorial mathematics
Number of schemes for finding composite conditions , So that all prime numbers are placed on the prime index , All composite numbers are placed on the composite index , Prime number placement and composite number placement are independent of each other , The total number of schemes is 【 The number of schemes in which all prime numbers are placed on the prime index 】*【 The number of schemes in which all composite numbers are placed on the composite index 】. seek 【 The number of schemes in which all prime numbers are placed on the prime index 】, That is, find the number of prime numbers numPrimes The factorial .【 The number of schemes in which all composite numbers are placed on the composite index 】 Empathy . When finding the number of primes , You can use trial division .【204. Official solution of counting prime numbers 】 Enumerate more methods of finding the number of prime numbers , Finally, pay attention to the need for 109+7 modulus .
Code
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++;
}
}
// Why are prime number placement and composite number placement independent of each other , The total number of schemes is
long total = factorial(numPrimes) * factorial(n - numPrimes);
long totalMod = total % MOD;
return (int) totalMod;
}
/** * Is it a prime number */
public boolean isPrime(int n) {
if (n == 1) {
return false;
}
for (int i = 2; i * i <= n; i++) {
// Why i * i <= n
if (n % i == 0) {
return false;
}
}
return true;
}
/** * Factorial */
public long factorial(int n) {
long res = 1;
for (int i = 1; i <= n; i++) {
res *= i;
res %= MOD;
}
return res;
}
}
Reprint
source : Power button (LeetCode)
link :https://leetcode.cn/problems/prime-arrangements
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
边栏推荐
- Brief analysis of tensorboard visual processing cases
- Ocean CMS vulnerability - search php
- Field problems in MySQL
- Road construction issues
- 网上开户哪家证券公司佣金最低,我要开户,网上客户经理开户安全吗
- Disruptor -- a high concurrency and high performance queue framework for processing tens of millions of levels
- rxjs Observable filter Operator 的实现原理介绍
- Logseq 评测:优点、缺点、评价、学习教程
- The latest BSC can pay dividends. Any B usdt Shib eth dividend destruction marketing can
- Unity render streaming communicates with unity through JS
猜你喜欢

Screenshot of the operation steps of upload labs level 4-level 9

Flutter dynamic | fair 2.5.0 new version features

NFT新的契机,多媒体NFT聚合平台OKALEIDO即将上线

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

User and group command exercises

JSP and filter

SQL Injection (GET/Search)
![[机缘参悟-37]:人感官系统的结构决定了人类是以自我为中心](/img/06/b71b505c7072d540955fda6da1dc1b.jpg)
[机缘参悟-37]:人感官系统的结构决定了人类是以自我为中心

PowerPoint 教程,如何在 PowerPoint 中将演示文稿另存为视频?

DQL basic query
随机推荐
PowerPoint 教程,如何在 PowerPoint 中将演示文稿另存为视频?
顺序表(C语言实现)
【被动收入如何挣个一百万】
MySQL constraints
Servlet
Replace the GPU card number when pytorch loads the historical model, map_ Location settings
Flutter dynamic | fair 2.5.0 new version features
Logseq 评测:优点、缺点、评价、学习教程
json序列化时案例总结
物联网毕设 --(STM32f407连接云平台检测数据)
Go language unit test 4: go language uses gomonkey to test functions or methods
Comprehensively develop the main channel of digital economy and digital group, and actively promote the utonmos digital Tibet market
CVPR 2022 | 美团技术团队精选6篇优秀论文解读
AI 考高数得分 81,网友:AI 模型也免不了“内卷”!
Go language unit test 3: go language uses gocovey library to do unit test
使用Tensorflow进行完整的深度神经网络CNN训练完成图片识别案例2
SVN添加文件时的错误处理:…\conf\svnserve.conf:12: Option expected
[技术发展-24]:现有物联网通信技术特点
[技術發展-24]:現有物聯網通信技術特點
Introduction to the implementation principle of rxjs observable filter operator