当前位置:网站首页>Force deduction solution summary 1175 prime number arrangement
Force deduction solution summary 1175 prime number arrangement
2022-07-22 19:32:00 【Lost summer】
Directory links :
Force buckle programming problem - The solution sums up _ Share + Record -CSDN Blog
GitHub Synchronous question brushing items :
https://github.com/September26/java-algorithms
Original link : Power button
describe :
Please help me to 1 To n Number design arrangement scheme , Make all of 「 Prime number 」 Should be placed in 「 Prime index 」( Index from 1 Start ) On ; You need to return the total number of possible solutions .
Let's review 「 Prime number 」: The prime number must be greater than 1 Of , And it cannot be expressed by the product of two positive integers less than it .
Because the answer could be big , So please return to the answer model mod 10^9 + 7 Then the result is .
Example 1:
Input :n = 5
Output :12
explain : for instance ,[1,2,5,4,3] Is an effective arrangement , but [5,2,3,4,1] No , Because in the second case, prime numbers 5 It is wrongly placed in the index as 1 Location .
Example 2:
Input :n = 100
Output :682289015
Tips :
1 <= n <= 100
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 .
Their thinking :
* Their thinking : * Find the quantity of prime and non prime numbers , Then sort them separately . The product is the total number of schemes .
Code :
public class Solution1175 {
public int numPrimeArrangements(int n) {
long primeNum = 0;
for (long i = 1; i <= n; i++) {
if (isPrime(i)) {
primeNum++;
}
}
long result = 1;
for (long i = 2; i <= primeNum; i++) {
result = ramainder(result * i, 10_0000_0000 + 7);
}
for (long i = 2; i <= (n - primeNum); i++) {
result = ramainder(result * i, 10_0000_0000 + 7);
}
return (int) result;
}
private boolean isPrime(long k) {
if (k < 2) {
return false;
}
for (int i = 2; i < k; i++) {
if (k % i == 0) {
return false;
}
}
return true;
}
// Modulus operation
public static long ramainder(long dividend, long dividor) {
return dividend % dividor;
}
}
边栏推荐
- DOM introduction and query
- JS advanced - lexical scope
- 4G工业路由器大气环境监测方案
- Constructor
- Leetcode daily question 2022/3/28-2022/4/3
- Behind the explosion of collaborative office market: cloud communication capability is the focus of demand
- Idea Quick Start Guide
- Methods of wrapping classes and strings
- LeetCode 每日一题 2021/12/6-2021/12/12
- Flutter 第一个程序Hello World!
猜你喜欢
Swagger UI introduction and common notes
Don't let fear of marriage kill your happiness!
关于人力外包公司那些事
Introduction to arrays
JS advanced - understanding of functions
[yolov5 practice 4] traffic sign recognition system based on yolov5 - model test and evaluation
PHP implementation deletes a value in a one-dimensional array
Swagger-UI介绍及常用注解说明
grafana面板-关于转换
Terminal data protection of Internet communication security
随机推荐
Oracle容器数据库的安装和使用
How can one page nest another page in thymeleaf? About page nesting, the tag tells you what you should know
How to add funding information when writing a paper with latex
LeetCode 每日一题 2022/1/3-2022/1/9
Methods of wrapping classes and strings
About human resource outsourcing companies
Grafana panel - override field values
大佬们,在使用flinksql时候使用rocksdb需要配置什么吗?或者需要什么jar包吗
LeetCode 每日一题 2022/3/7-2022/3/13
Don't let fear of marriage kill your happiness!
Bean 的作用域和生命周期
30出头成为复旦博导,陈思明:敲代码和写诗,我两样都要
融云 x 幸识: 专属 00 后的真我社交自留地(内含抽奖)
融云办政事: “小网格”也能实现“大治理”
LeetCode 每日一题 2021/11/22-2021/11/28
Leetcode daily question 2021/12/27-2022/1/2
Leetcode: 1179. Reformat department table
Fluent adjusts the drawing shape by dragging
河北、浙江的气候变化了?
LeetCode 每日一题 2021/12/6-2021/12/12