当前位置:网站首页>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 .
边栏推荐
- Red Hat Satellite 6:更好地管理服务器和云
- Task6: using transformer for emotion analysis
- [sort] bucket sort
- rxjs Observable filter Operator 的实现原理介绍
- JS 将伪数组转换成数组
- ThreadPoolExecutor realizes multi-threaded concurrency and obtains the return value (elegant and concise way)
- Comprehensively develop the main channel of digital economy and digital group, and actively promote the utonmos digital Tibet market
- SQL Injection (AJAX/JSON/jQuery)
- Go language unit test 4: go language uses gomonkey to test functions or methods
- Realize the recognition and training of CNN images, and process the cifar10 data set and other methods through the tensorflow framework
猜你喜欢

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

【电脑插入U盘或者内存卡显示无法格式化FAT32如何解决】

Flutter dynamic | fair 2.5.0 new version features

MyCms 自媒体商城 v3.4.1 发布,使用手册更新

PowerPoint tutorial, how to save a presentation as a video in PowerPoint?

SQL Injection (GET/Select)

KEIL5出现中文字体乱码的解决方法

rxjs Observable filter Operator 的实现原理介绍

全面发展数字经济主航道 和数集团积极推动UTONMOS数藏市场

Unity embeddedbrowser browser plug-in event communication
随机推荐
使用tensorflow进行完整的DNN深度神经网络CNN训练完成图片识别案例
Resolved (error in viewing data information in machine learning) attributeerror: target_ names
mysql更新时条件为一查询
Software testing is so hard to find, only outsourcing offers, should I go?
Use docker to build sqli lab environment and upload labs environment, and the operation steps are provided with screenshots.
Realize the recognition and training of CNN images, and process the cifar10 data set and other methods through the tensorflow framework
Comprehensive evaluation of double chain notes remnote: fast input, PDF reading, interval repetition / memory
[how to solve FAT32 when the computer is inserted into the U disk or the memory card display cannot be formatted]
[技术发展-24]:现有物联网通信技术特点
Convolution emotion analysis task4
[today in history] July 3: ergonomic standards act; The birth of pioneers in the field of consumer electronics; Ubisoft releases uplay
SQL Injection (GET/Select)
The network card fails to start after the cold migration of the server hard disk
JS convert pseudo array to array
Task6: using transformer for emotion analysis
[quantitative trading] permanent portfolio, turtle trading rules reading, back testing and discussion
记录关于银行回调post请求405 问题
SQL Injection (POST/Search)
全面发展数字经济主航道 和数集团积极推动UTONMOS数藏市场
使用Tensorflow进行完整的深度神经网络CNN训练完成图片识别案例2