当前位置:网站首页>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 .
边栏推荐
- Spark practice 1: build spark operation environment in single node local mode
- Box layout of Kivy tutorial BoxLayout arranges sub items in vertical or horizontal boxes (tutorial includes source code)
- When updating mysql, the condition is a query
- The network card fails to start after the cold migration of the server hard disk
- MapReduce implements matrix multiplication - implementation code
- Resolved (error in viewing data information in machine learning) attributeerror: target_ names
- 全面发展数字经济主航道 和数集团积极推动UTONMOS数藏市场
- SQL Injection (GET/Select)
- Resource Cost Optimization Practice of R & D team
- Complete DNN deep neural network CNN training with tensorflow to complete image recognition cases
猜你喜欢

Resource Cost Optimization Practice of R & D team

太阳底下无新事,元宇宙能否更上层楼?

Complete deep neural network CNN training with tensorflow to complete picture recognition case 2

8皇后问题

TensorBoard可视化处理案例简析

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

挡不住了,国产芯片再度突进,部分环节已进到4nm

Kivy教程之 如何自动载入kv文件
![[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]

Screenshot of the operation steps of upload labs level 4-level 9
随机推荐
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
pytorch 载入历史模型时更换gpu卡号,map_location设置
Multi table query of MySQL - multi table relationship and related exercises
KEIL5出现中文字体乱码的解决方法
[机缘参悟-37]:人感官系统的结构决定了人类是以自我为中心
Spark practice 1: build spark operation environment in single node local mode
Comprehensive evaluation of double chain notes remnote: fast input, PDF reading, interval repetition / memory
刚毕业的欧洲大学生,就能拿到美国互联网大厂 Offer?
Setting up remote links to MySQL on Linux
[技术发展-24]:现有物联网通信技术特点
Libuv Library - Design Overview (Chinese version)
Kivy tutorial how to load kV file design interface by string (tutorial includes source code)
【被动收入如何挣个一百万】
编程内功之编程语言众多的原因
windos 创建cordova 提示 因为在此系统上禁止运行脚本
The R language GT package and gtextras package gracefully and beautifully display tabular data: nflreadr package and gt of gtextras package_ plt_ The winloss function visualizes the win / loss values
研发团队资源成本优化实践
Road construction issues
使用tensorflow进行完整的DNN深度神经网络CNN训练完成图片识别案例
IBEM mathematical formula detection data set