当前位置:网站首页>LeetCode 1175. Prime number arrangement (prime number judgment + Combinatorial Mathematics)
LeetCode 1175. Prime number arrangement (prime number judgment + Combinatorial Mathematics)
2022-07-02 05:04:00 【xylitolz】
List of articles
0 subject
1 Their thinking
1.1 Prime number judgment + Combinatorial mathematics
Ben topic total Of Fang case Count = 「 the Yes quality Count all discharge stay quality Count Cable lead On Of Fang case Count 」 × 「 the Yes close Count all discharge stay close Count Cable lead On Of Fang case Count 」 Total number of schemes in this question =「 The number of schemes in which all prime numbers are placed on the prime index 」\times \\ 「 The number of schemes in which all composite numbers are placed on the composite index 」 Ben topic total Of Fang case Count =「 the Yes quality Count all discharge stay quality Count Cable lead On Of Fang case Count 」×「 the Yes close Count all discharge stay close Count Cable lead On Of Fang case Count 」
「 The number of schemes in which all prime numbers are placed on the prime index 」:
- First of all find out [ 1 , n ] [1,n] [1,n] The number of all prime numbers in the range p r i m e N u m primeNum primeNum
- enumeration 100 All prime numbers within + Two points
- Trial division
- Then find the number of prime numbers primeNum \textit{primeNum} primeNum Of Factorial is the number of schemes
「 The number of schemes in which all composite numbers are placed on the composite index 」:
- n − p r i m e N u m n - primeNum n−primeNum That is, the number of all composite numbers , Then the factorial is the number of schemes
array :
A ( m , n ) = n ! ( n − m ) ! A(m, n) = \frac{n!}{(n - m)!} A(m,n)=(n−m)!n!Combine :
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 Code implementation
1.1.1.1 enumeration 100 All prime numbers within + Two points
class Solution {
// enumeration 100 All prime numbers within
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) {
// Use bisection to find the number of prime numbers
int primeNum = binarySearch(n) + 1;
// Use factorial to find the number of schemes
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 Trial division
class Solution {
private static final int MOD = 1000000007;
public int numPrimeArrangements(int n) {
// Try division to get the number of prime numbers
int primeNum = 0;
for (int i = 1; i <= n; i++) {
if (isPrime(i)) {
primeNum++;
}
}
// Use factorial to find the number of schemes
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
边栏推荐
- [Yu Yue education] autumn 2021 reference materials of Tongji University
- 正大留4的主账户信息汇总
- Express logistics quick query method, set the unsigned doc No. to refresh and query automatically
- [high speed bus] Introduction to jesd204b
- 国产全中文-自动化测试软件Apifox
- DJB Hash
- Simple and practical accounting software, so that accounts can be checked
- Application d'un robot intelligent dans le domaine de l'agroécologie
- Change deepin to Alibaba image source
- 数据库问题汇总
猜你喜欢
paddle: ValueError:quality setting only supported for ‘jpeg‘ compression
面试会问的 Promise.all()
Application d'un robot intelligent dans le domaine de l'agroécologie
AcrelEMS高速公路微电网能效管理平台与智能照明解决方案智慧点亮隧道
Video multiple effects production, fade in effect and border background are added at the same time
How to configure PostgreSQL 12.9 to allow remote connections
Super detailed pycharm tutorial
Summary of main account information of zhengdaliu 4
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
Mathematical knowledge (Euler function)
随机推荐
Pytest learning ----- pytest assertion of interface automation testing
Using Kube bench and Kube hunter to evaluate the risk of kubernetes cluster
2022阿里巴巴全球数学竞赛 第4题 虎虎生威(盲盒问题、集卡问题)解决思路
Getting started with pytest ----- confitest Application of PY
Pyechart1.19 national air quality exhibition
DJB Hash
LeetCode 1175. 质数排列(质数判断+组合数学)
How to configure PostgreSQL 12.9 to allow remote connections
Cultivate primary and secondary school students' love for educational robots
Exercise notes 13 (effective letter ectopic words)
Comp 250 parsing
【ClickHouse】How to create index for Map Type Column or one key of it?
Win10 disk management compressed volume cannot be started
Cubemx DMA notes
Video cover image setting, put cover images into multiple videos in the simplest way
農業生態領域智能機器人的應用
Rhcsa --- work on the third day
Case sharing | intelligent Western Airport
Analyzing the hands-on building tutorial in children's programming
el-cascader回显只选中不显示的问题