当前位置:网站首页>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
边栏推荐
- js面试收藏试题1
- Here comes the chicken soup! Keep this quick guide for data analysts
- Express logistics quick query method, set the unsigned doc No. to refresh and query automatically
- Change deepin to Alibaba image source
- How to write a client-side technical solution
- The reason why sizeof (ARR) / sizeof (arr[0]) is used in the function to calculate the length of the array is incorrect
- AcrelEMS高速公路微电网能效管理平台与智能照明解决方案智慧点亮隧道
- [opencv] image binarization
- 2022阿里巴巴全球数学竞赛 第4题 虎虎生威(盲盒问题、集卡问题)解决思路
- Interview question: do you know the difference between deep copy and shallow copy? What is a reference copy?
猜你喜欢
10 minute quick start UI automation ----- puppeter
How to write a client-side technical solution
Detailed process of DC-1 range construction and penetration practice (DC range Series)
農業生態領域智能機器人的應用
Embedded-c language-9-makefile/ structure / Consortium
Tawang food industry insight | current situation, consumption data and trend analysis of domestic infant complementary food market
Rhcsa --- work on the third day
2022阿里巴巴全球数学竞赛 第4题 虎虎生威(盲盒问题、集卡问题)解决思路
Using Kube bench and Kube hunter to evaluate the risk of kubernetes cluster
[Yu Yue education] autumn 2021 reference materials of Tongji University
随机推荐
Implementation of go language for deleting duplicate items in sorting array
DMA Porter
DC-1靶场搭建及渗透实战详细过程(DC靶场系列)
leetcode两数相加go实现
10 minute quick start UI automation ----- puppeter
Cultivate primary and secondary school students' love for educational robots
Analyzing the hands-on building tutorial in children's programming
js面试收藏试题1
关于Steam 教育的知识整理
How to recover deleted data in disk
Idea autoguide package and autodelete package Settings
Beginner crawler - biqu Pavilion crawler
洛谷入门3【循环结构】题单题解
Exercise notes 13 (effective letter ectopic words)
C case of communication between server and client based on mqttnet
数学知识(欧拉函数)
LM09丨费雪逆变换反转网格策略
Acelems Expressway microgrid energy efficiency management platform and intelligent lighting solution intelligent lighting tunnel
Getting started with pytest -- description of fixture parameters
Mapping settings in elk (8) es