当前位置:网站首页>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 .
边栏推荐
- 栈应用(平衡符)
- 使用tensorflow进行完整的DNN深度神经网络CNN训练完成图片识别案例
- JSON serialization case summary
- Complete deep neural network CNN training with tensorflow to complete picture recognition case 2
- Mysql database basic operation - regular expression
- 8 Queen question
- Screenshot of the operation steps of upload labs level 4-level 9
- 8皇后问题
- 刚毕业的欧洲大学生,就能拿到美国互联网大厂 Offer?
- 【BW16 应用篇】安信可BW16模组与开发板更新固件烧录说明
猜你喜欢
mysql更新时条件为一查询
Comprehensive evaluation of double chain notes remnote: fast input, PDF reading, interval repetition / memory
[technology development-24]: characteristics of existing IOT communication technology
This math book, which has been written by senior ml researchers for 7 years, is available in free electronic version
[quantitative trading] permanent portfolio, turtle trading rules reading, back testing and discussion
106. How to improve the readability of SAP ui5 application routing URL
Several common optimization methods matlab principle and depth analysis
Can newly graduated European college students get an offer from a major Internet company in the United States?
MyCms 自媒体商城 v3.4.1 发布,使用手册更新
Unable to stop it, domestic chips have made another breakthrough, and some links have reached 4nm
随机推荐
The difference between stratifiedkfold (classification) and kfold (regression)
Setting up remote links to MySQL on Linux
logback日志的整理
Red hat satellite 6: better management of servers and clouds
Tutoriel PowerPoint, comment enregistrer une présentation sous forme de vidéo dans Powerpoint?
Flutter dynamic | fair 2.5.0 new version features
Go language unit test 3: go language uses gocovey library to do unit test
Disruptor -- a high concurrency and high performance queue framework for processing tens of millions of levels
NFT new opportunity, multimedia NFT aggregation platform okaleido will be launched soon
Shell timing script, starting from 0, CSV format data is regularly imported into PostgreSQL database shell script example
JS convert pseudo array to array
File uploading and email sending
[how to earn a million passive income]
PhpMyAdmin stage file contains analysis traceability
Ubuntu 14.04 下开启PHP错误提示
软件测试工作那么难找,只有外包offer,我该去么?
Mastering the cypress command line options is the basis for truly mastering cypress
The latest BSC can pay dividends. Any B usdt Shib eth dividend destruction marketing can
KEIL5出现中文字体乱码的解决方法
使用tensorflow进行完整的DNN深度神经网络CNN训练完成图片识别案例