当前位置:网站首页>LeetCode 1175. 质数排列(质数判断+组合数学)
LeetCode 1175. 质数排列(质数判断+组合数学)
2022-07-02 05:01:00 【xylitolz】
0 题目

1 解题思路
1.1 质数判断+组合数学
本 题 总 的 方 案 数 = 「 所 有 质 数 都 放 在 质 数 索 引 上 的 方 案 数 」 × 「 所 有 合 数 都 放 在 合 数 索 引 上 的 方 案 数 」 本题总的方案数=「所有质数都放在质数索引上的方案数」\times \\ 「所有合数都放在合数索引上的方案数」 本题总的方案数=「所有质数都放在质数索引上的方案数」×「所有合数都放在合数索引上的方案数」
「所有质数都放在质数索引上的方案数」:
- 首先求出 [ 1 , n ] [1,n] [1,n]范围内所有质数的个数 p r i m e N u m primeNum primeNum
- 枚举100以内所有质数+二分
- 试除法
- 再求出质数个数 primeNum \textit{primeNum} primeNum 的阶乘即为方案数
「所有合数都放在合数索引上的方案数」:
- n − p r i m e N u m n - primeNum n−primeNum即为所有合数的个数,再求其阶乘即为方案数
排列:
A ( m , n ) = n ! ( n − m ) ! A(m, n) = \frac{n!}{(n - m)!} A(m,n)=(n−m)!n!组合:
C ( m , n ) = n ! m ! × ( n − m ) ! C(m, n) = \frac{n!}{m! \times (n - m)!} C(m,n)=m!×(n−m)!n!
1.1.1 代码实现
1.1.1.1 枚举100以内所有质数+二分
class Solution {
// 枚举100以内所有质数
private static int[] PRIME = {
2, 3, 5, 7,
11, 13, 17, 19,
23, 29,
31, 37,
41, 43, 47,
53, 59,
61, 67,
71, 73, 79,
83, 89,
97};
private static final int MOD = 1000000007;
public int numPrimeArrangements(int n) {
// 利用二分求取质数个数
int primeNum = binarySearch(n) + 1;
// 利用阶乘求方案数
return (int) (calcFactorial(primeNum) * calcFactorial(n - primeNum) % MOD) ;
}
private long calcFactorial(int m) {
long res = 1;
while (m != 0) {
res *= m;
res %= MOD;
m--;
}
return res;
}
private int binarySearch(int n) {
int left = 0, right = PRIME.length - 1;
while (left < right) {
int mid = left + (right - left + 1) / 2;
if (PRIME[mid] > n) {
right = mid - 1;
} else {
left = mid;
}
}
return left;
}
}
1.1.1.2 试除法
class Solution {
private static final int MOD = 1000000007;
public int numPrimeArrangements(int n) {
// 试除法求取质数个数
int primeNum = 0;
for (int i = 1; i <= n; i++) {
if (isPrime(i)) {
primeNum++;
}
}
// 利用阶乘求方案数
return (int) (calcFactorial(primeNum) * calcFactorial(n - primeNum) % MOD);
}
public boolean isPrime(int n) {
if (n == 1) {
return false;
}
for (int i = 2; i * i <= n; i++) {
if (n % i == 0) {
return false;
}
}
return true;
}
public long calcFactorial(int n) {
long res = 1;
for (int i = 1; i <= n; i++) {
res *= i;
res %= MOD;
}
return res;
}
}
2 Reference
边栏推荐
- 数学知识(欧拉函数)
- Pyflink writes MySQL examples with JDBC
- Interview question: do you know the difference between deep copy and shallow copy? What is a reference copy?
- JS interview collection test question 1
- 06 decorator mode
- Oracle和MySQL的基本区别(入门级)
- Analyze the space occupied by the table according to segments, clusters and pages
- Getting started with pytest ----- confitest Application of PY
- 初学爬虫-笔趣阁爬虫
- 國產全中文-自動化測試軟件Apifox
猜你喜欢

UNET deployment based on deepstream

Starting from the classification of database, I understand the map database

Interview question: do you know the difference between deep copy and shallow copy? What is a reference copy?

Cannot activate CONDA virtual environment in vscode
![Introduction to Luogu 3 [circular structure] problem list solution](/img/fd/c0c5687c7e6e74bd5c911b27c3e19c.png)
Introduction to Luogu 3 [circular structure] problem list solution

Acelems Expressway microgrid energy efficiency management platform and intelligent lighting solution intelligent lighting tunnel

AcrelEMS高速公路微电网能效管理平台与智能照明解决方案智慧点亮隧道

Rhcsa --- work on the fourth day

Analyze the space occupied by the table according to segments, clusters and pages

Cubemx DMA notes
随机推荐
Detailed process of DC-1 range construction and penetration practice (DC range Series)
国产全中文-自动化测试软件Apifox
Rhcsa --- work on the third day
Domestic all Chinese automatic test software apifox
Lay the foundation for children's programming to become a basic discipline
Acelems Expressway microgrid energy efficiency management platform and intelligent lighting solution intelligent lighting tunnel
List of common bugs in software testing
[bus interface] Axi interface
Mapping settings in elk (8) es
win10 磁盘管理 压缩卷 无法启动问题
Lm09 Fisher inverse transform inversion mesh strategy
解析少儿编程中的动手搭建教程
How to modify data file path in DM database
面试会问的 Promise.all()
Knowledge arrangement about steam Education
Line by line explanation of yolox source code of anchor free series network (7) -- obj in head_ loss、Cls_ Loss and reg_ Calculation and reverse transmission of loss I
Cultivate primary and secondary school students' love for educational robots
Ruby replaces gem Alibaba image
geotrust ov多域名ssl證書一年兩千一百元包含幾個域名?
C# 图片显示占用问题