当前位置:网站首页>Leetcode-1175. Prime Arrangements
Leetcode-1175. Prime Arrangements
2022-07-03 13:47:00 【Eistert】
subject
Return the number of permutations of 1 to n so that prime numbers are at prime indices (1-indexed.)
(Recall that an integer is prime if and only if it is greater than 1, and cannot be written as a product of two positive integers both smaller than it.)
Since the answer may be large, return the answer modulo 109 + 7.
permutations [ Count ] array (permutation Complex number )
modulo modulus
solution
Method 1 : Prime number judgment + Combinatorial mathematics
Number of schemes for finding composite conditions , So that all prime numbers are placed on the prime index , All composite numbers are placed on the composite index , Prime number placement and composite number placement are independent of each other , The total number of schemes is 【 The number of schemes in which all prime numbers are placed on the prime index 】*【 The number of schemes in which all composite numbers are placed on the composite index 】. seek 【 The number of schemes in which all prime numbers are placed on the prime index 】, That is, find the number of prime numbers numPrimes The factorial .【 The number of schemes in which all composite numbers are placed on the composite index 】 Empathy . When finding the number of primes , You can use trial division .【204. Official solution of counting prime numbers 】 Enumerate more methods of finding the number of prime numbers , Finally, pay attention to the need for 109+7 modulus .
Code
package com.leetcode.question.medium;
/** * 1175 Prime Arrangements * * @ClassName PrimeArrangements1175 * @Author eistert * @Date 2022/7/1 18:01 **/
public class PrimeArrangements1175 {
public static void main(String[] args) {
}
static final int MOD = 1000000007;
public int numPrimeArrangements(int n) {
int numPrimes = 0;
for (int i = 1; i <= n; i++) {
if (isPrime(i)) {
numPrimes++;
}
}
// Why are prime number placement and composite number placement independent of each other , The total number of schemes is
long total = factorial(numPrimes) * factorial(n - numPrimes);
long totalMod = total % MOD;
return (int) totalMod;
}
/** * Is it a prime number */
public boolean isPrime(int n) {
if (n == 1) {
return false;
}
for (int i = 2; i * i <= n; i++) {
// Why i * i <= n
if (n % i == 0) {
return false;
}
}
return true;
}
/** * Factorial */
public long factorial(int n) {
long res = 1;
for (int i = 1; i <= n; i++) {
res *= i;
res %= MOD;
}
return res;
}
}
Reprint
source : Power button (LeetCode)
link :https://leetcode.cn/problems/prime-arrangements
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
边栏推荐
- Disruptor -- a high concurrency and high performance queue framework for processing tens of millions of levels
- 又一个行业被中国芯片打破空白,难怪美国模拟芯片龙头降价抛售了
- Can newly graduated European college students get an offer from a major Internet company in the United States?
- 双向链表(我们只需要关注插入和删除函数)
- JS 将伪数组转换成数组
- [sort] bucket sort
- Servlet
- Mysql database basic operation - regular expression
- R language uses the data function to obtain the sample datasets available in the current R environment: obtain all the sample datasets in the datasets package, obtain the datasets of all packages, and
- IBEM mathematical formula detection data set
猜你喜欢
Brief analysis of tensorboard visual processing cases
用户和组命令练习
Smbms project
SQL Injection (GET/Select)
Go language unit test 3: go language uses gocovey library to do unit test
[today in history] July 3: ergonomic standards act; The birth of pioneers in the field of consumer electronics; Ubisoft releases uplay
物联网毕设 --(STM32f407连接云平台检测数据)
MyCms 自媒体商城 v3.4.1 发布,使用手册更新
Setting up remote links to MySQL on Linux
Unable to stop it, domestic chips have made another breakthrough, and some links have reached 4nm
随机推荐
Shell timing script, starting from 0, CSV format data is regularly imported into PostgreSQL database shell script example
[technology development-24]: characteristics of existing IOT communication technology
Bidirectional linked list (we only need to pay attention to insert and delete functions)
记录关于银行回调post请求405 问题
又一个行业被中国芯片打破空白,难怪美国模拟芯片龙头降价抛售了
Logback log sorting
Logseq evaluation: advantages, disadvantages, evaluation, learning tutorial
Flutter动态化 | Fair 2.5.0 新版本特性
Father and basketball
json序列化时案例总结
Flutter dynamic | fair 2.5.0 new version features
Road construction issues
Field problems in MySQL
Task6: using transformer for emotion analysis
windos 创建cordova 提示 因为在此系统上禁止运行脚本
全面发展数字经济主航道 和数集团积极推动UTONMOS数藏市场
Leetcode-1175.Prime Arrangements
网上开户哪家证券公司佣金最低,我要开户,网上客户经理开户安全吗
AI 考高数得分 81,网友:AI 模型也免不了“内卷”!
Anan's doubts