当前位置:网站首页>[daily question] 1175 Prime permutation
[daily question] 1175 Prime permutation
2022-07-01 16:05:00 【Wang Liuliu's it daily】
I feel that I don't practice much in this field .
Make a summary ...
204. Count prime
subject :
Given integer n , return All integers less than nonnegative n The number of prime numbers .
Definition of prime number : In more than 1 Of the natural number , except 1 And a natural number that has no more factors than itself .
- enumeration
class Solution {
public int countPrimes(int n) {
int ans = 0;
for (int i = 2; i < n; ++i) {
ans += isPrime(i) ? 1 : 0;
}
return ans;
}
public boolean isPrime(int x) {
for (int i = 2; i * i <= x; ++i) {
if (x % i == 0) {
return false;
}
}
return true;
}
}
- Ethmoid :https://leetcode.cn/problems/count-primes/solution/nick-by-nickbean-6ej3/、https://leetcode.cn/problems/count-primes/solution/kuai-lai-miao-dong-shai-zhi-shu-by-sweetiee/
If a number A Prime number , that 2A,3A,…,n*A It must not be a prime number
So it can be [1,n] In this range , From the very beginning 2,3 Start initializing the number after the prime number !
class Solution {
public int countPrimes(int n) {
int[] isPrime = new int[n];
// fill isPrime Every element of an array is 1
Arrays.fill(isPrime, 1);
int count = 0;
for (int i = 2; i < n; ++i) {
if (isPrime[i] == 1) {
/** * explain i Prime number ! */
count++;
if ((long) i * i < n) {
for (int j = i * i; j < n; j += i) {
isPrime[j] = 0;
}
}
}
}
return count;
}
}
There is a better way to sift this question —— Linear sieve , But it doesn't belong to the written examination / Interview scope , There is no need to master .
- Linear sieve
class Solution {
public int countPrimes(int n) {
List<Integer> primes = new ArrayList<Integer>();
int[] isPrime = new int[n];
Arrays.fill(isPrime, 1);
for (int i = 2; i < n; ++i) {
if (isPrime[i] == 1) {
primes.add(i);
}
for (int j = 0; j < primes.size() && i * primes.get(j) < n; ++j) {
isPrime[i * primes.get(j)] = 0;
if (i % primes.get(j) == 0) {
break;
}
}
}
return primes.size();
}
}
1175. Permutation of prime numbers
[ mathematics + Prime number + Factorial ]
Refer to the explanation of the question :https://leetcode.cn/problems/prime-arrangements/solution/1175-zhi-shu-pai-lie-java-100-by-wa-pian-bo17/
- [1,n] How many primes are there
- ans == Prime number !* Nonprime number !
According to the meaning , The problem can be transformed into a solution n The number of prime numbers within , Write it down as a, At the same time, the number of non prime numbers is b = n - a.
The number of placement schemes for prime numbers is a!, The number of placement schemes for non prime numbers is b!, according to 「 Multiplication principle 」 The total number of placement schemes is a!×b!.
class Solution {
int mod = (int) 1e9 + 7;
public int numPrimeArrangements(int n) {
int prime = 0;
for (int i = 1; i <= n; i++) {
if (isPrime(i)) {
prime++;
}
}
long ans = factorial(prime);
ans *= factorial(n - prime);
return (int) (ans % mod);
}
// Factorial
private long factorial(int num) {
long ans = 1;
for (int i = 1; i <= num; i++) {
ans *= (long) i;
ans %= mod;
}
return ans;
}
private boolean isPrime(int num) {
if (num == 2) {
return true;
}
if (num == 1 || num % 2 == 0) {
return false;
}
for (int i = 3; i * i <= num; i += 2) {
if (num % i == 0 || i * i == num) {
return false;
}
}
return true;
}
}
Reference resources :https://leetcode.cn/problems/prime-arrangements/solution/by-ac_oier-t3lk/
The meter + Two points + mathematics
Can pass 「 The meter 」 The way to 100 Primes within are preprocessed to arrays list in , For each n for , We find the first satisfaction 「 Value less than or equal to n」 The location of , So we know n The number of prime numbers within the range .
class Solution {
static int MOD = (int)1e9+7;
static List<Integer> list = new ArrayList<>();
static {
for (int i = 2; i <= 100; i++) {
boolean ok = true;
for (int j = 2; j * j <= i; j++) {
if (i % j == 0) ok = false;
}
if (ok) list.add(i);
}
}
public int numPrimeArrangements(int n) {
int l = 0, r = list.size() - 1;
while (l < r) {
int mid = l + r + 1 >> 1;
if (list.get(mid) <= n) l = mid;
else r = mid - 1;
}
int a = r + 1, b = n - a;
long ans = 1;
for (int i = b; i > 1; i--) ans = ans * i % MOD ;
for (int i = a; i > 1; i--) ans = ans * i % MOD ;
return (int)ans;
}
}
边栏推荐
- Samsung took the lead in putting 3nm chips into production, and Shanghai's fresh master students can settle directly. Nankai has established a chip science center. Today, more big news is here
- 2022 Moonriver全球黑客松优胜项目名单
- 最新NLP赛事实践总结!
- Zero copy technology of MySQL
- TensorFlow团队:我们没被抛弃
- 2022 Moonriver global hacker song winning project list
- 马来西亚《星报》:在WTO MC12 孙宇晨仍在坚持数字经济梦想
- Pico, do you want to save or bring consumer VR?
- MySQL的零拷贝技术
- 搜索框和按钮缩放时会有缝隙的bug
猜你喜欢

Automatic, intelligent and visual! Deeply convinced of the eight designs behind sslo scheme
![[daily news]what happened to the corresponding author of latex](/img/0f/d19b27dc42124c89993dee1bada838.png)
[daily news]what happened to the corresponding author of latex

普通二本,去过阿里外包,到现在年薪40W+的高级测试工程师,我的两年转行心酸经历...

TensorFlow團隊:我們沒被拋弃

Im instant messaging develops a message delivery scheme for 10000 people

新出生的机器狗,打滚1小时后自己掌握走路,吴恩达开山大弟子最新成果

Tanabata confession introduction: teach you to use your own profession to say love words, the success rate is 100%, I can only help you here ~ (programmer Series)

The newly born robot dog can walk by himself after rolling for an hour. The latest achievement of Wu Enda's eldest disciple

In the era of super video, what kind of technology will become the base?

Nuxt.js数据预取
随机推荐
Nuxt. JS data prefetching
[open source data] open source data set for cross modal (MRI, Meg, eye movement) human spatial memory research based on virtual reality scenes
新出生的机器狗,打滚1小时后自己掌握走路,吴恩达开山大弟子最新成果
Crypto Daily:孙宇晨在MC12上倡议用数字化技术解决全球问题
How to adjust the size of computer photos to what you want
ATSS:自动选择样本,消除Anchor based和Anchor free物体检测方法之间的差别
Pico, do you want to save or bring consumer VR?
智慧党建: 穿越时空的信仰 | 7·1 献礼
一次革命、两股力量、三大环节:《工业能效提升行动计划》背后的“减碳”路线图...
AVL 平衡二叉搜索树
Where should older test / development programmers go? Will it be abandoned by the times?
ABAP-调用Restful API
How does win11 set user permissions? Win11 method of setting user permissions
TensorFlow團隊:我們沒被拋弃
Programming examples of stm32f1 and stm32subeide - production melody of PWM driven buzzer
2023届春招实习-个人面试过程和面经分享
Win11如何設置用戶權限?Win11設置用戶權限的方法
【每日一题】1175. 质数排列
部门来了个拿25k出来的00后测试卷王,老油条表示真干不过,已被...
Learn selenium to simulate mouse operation, and you can be lazy a little bit