当前位置:网站首页>[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;
}
}
边栏推荐
- AVL 平衡二叉搜索树
- Do280 management application deployment - pod scheduling control
- ADS算力芯片的多模型架构研究
- 基于PHP的轻量企业销售管理系统
- How to adjust the color of the computer screen and how to change the color of the computer screen
- 电脑屏幕变色了怎么调回来,电脑屏幕颜色怎么改
- 【Hot100】17. 电话号码的字母组合
- ABAP call restful API
- 并发编程系列之什么是ForkJoin框架?
- 硬件开发笔记(九): 硬件开发基本流程,制作一个USB转RS232的模块(八):创建asm1117-3.3V封装库并关联原理图元器件
猜你喜欢
2022 Moonriver global hacker song winning project list
马来西亚《星报》:在WTO MC12 孙宇晨仍在坚持数字经济梦想
STM32F1与STM32CubeIDE编程实例-PWM驱动蜂鸣器生产旋律
七夕表白攻略:教你用自己的专业说情话,成功率100%,我只能帮你们到这里了啊~(程序员系列)
部门来了个拿25k出来的00后测试卷王,老油条表示真干不过,已被...
超视频时代,什么样的技术会成为底座?
Detailed explanation of stm32adc analog / digital conversion
[pyGame practice] do you think it's magical? Pac Man + cutting fruit combine to create a new game you haven't played! (source code attached)
u本位合约和币本位合约有区别,u本位合约会爆仓吗
Hardware development notes (9): basic process of hardware development, making a USB to RS232 module (8): create asm1117-3.3v package library and associate principle graphic devices
随机推荐
For the sustainable development of software testing, we must learn to knock code?
HR interview: the most common interview questions and technical answers
Go语学习笔记 - gorm使用 - 表增删改查 | Web框架Gin(八)
Win11如何設置用戶權限?Win11設置用戶權限的方法
[IDM] IDM downloader installation
跨平台应用开发进阶(二十四) :uni-app实现文件下载并保存
基于PHP的轻量企业销售管理系统
u本位合约和币本位合约有区别,u本位合约会爆仓吗
Zero copy technology of MySQL
【Hot100】19. 删除链表的倒数第 N 个结点
分享在大疆DJI(深圳总部)工作的日常和福利
idea启动Command line is too long问题处理
Comment win11 définit - il les permissions de l'utilisateur? Win11 comment définir les permissions de l'utilisateur
2022 Moonriver全球黑客松优胜项目名单
Crypto Daily: Sun Yuchen proposed to solve global problems with digital technology on MC12
表格存储中tablestore 目前支持mysql哪些函数呢?
VIM from dislike to dependence (22) -- automatic completion
How to adjust the color of the computer screen and how to change the color of the computer screen
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)
ssm框架原理