当前位置:网站首页>[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;
}
}
边栏推荐
- 【每日一题】1175. 质数排列
- 【IDM】IDM下载器安装
- Which MySQL functions are currently supported by tablestore in table storage?
- Tensorflow team: we haven't been abandoned
- 学会了selenium 模拟鼠标操作,你就可以偷懒点点点了
- 圈铁发音,动感与无噪强强出彩,魔浪HIFIair蓝牙耳机测评
- Sales management system of lightweight enterprises based on PHP
- 韩国AI团队抄袭震动学界!1个导师带51个学生,还是抄袭惯犯
- ThinkPHP advanced
- Go语学习笔记 - gorm使用 - 表增删改查 | Web框架Gin(八)
猜你喜欢
She is the "HR of others" | ones character
新出生的机器狗,打滚1小时后自己掌握走路,吴恩达开山大弟子最新成果
揭秘慕思“智商税”:狂砸40亿搞营销,发明专利仅7项
RT-Thread Env 工具介绍(学习笔记)
Zhou Shaojian, rare
The newly born robot dog can walk by himself after rolling for an hour. The latest achievement of Wu Enda's eldest disciple
C#/VB. Net merge PDF document
大龄测试/开发程序员该何去何从?是否会被时代抛弃?
Gaussdb (for MySQL):partial result cache, which accelerates the operator by caching intermediate results
Please, stop painting star! This has nothing to do with patriotism!
随机推荐
MySQL的零拷贝技术
Share the daily work and welfare of DJI (Shenzhen headquarters) in Dajiang
2023 spring recruitment Internship - personal interview process and face-to-face experience sharing
Pico,能否拯救消费级VR?
Smart Party Building: faith through time and space | 7.1 dedication
Nuxt. JS data prefetching
[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)
[daily news]what happened to the corresponding author of latex
#夏日挑战赛# HarmonyOS canvas实现时钟
ssm框架原理
What is the forkjoin framework in the concurrent programming series?
process. env. NODE_ ENV
Overview | slam of laser and vision fusion
马来西亚《星报》:在WTO MC12 孙宇晨仍在坚持数字经济梦想
Embedded development: five revision control best practices
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
【OpenCV 例程200篇】216. 绘制多段线和多边形
vscode 查找 替换 一个文件夹下所有文件的数据
laravel的模型删除后动作
u本位合约和币本位合约有区别,u本位合约会爆仓吗