当前位置:网站首页>Force deduction solution summary 1175- prime number arrangement
Force deduction solution summary 1175- prime number arrangement
2022-06-30 18:14: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;
}
}边栏推荐
- ABAP-发布Restful服务
- Elastic 8.0: opening a new era of speed, scale, relevance and simplicity
- Small tools (3) integration knife4j3.0.3 interface document
- 基于SSH的通讯网络电子计费系统
- Development details of NFT casting trading platform
- Spin lock exploration
- 墨天轮沙龙 | 清华乔嘉林:Apache IoTDB,源于清华,建设开源生态之路
- 【剑指Offer】53 - I. 在排序数组中查找数字 I
- MIT科技评论2022年35岁以下创新者名单发布,含AlphaFold作者等
- 基于SSM的新闻管理系统
猜你喜欢

Compile and generate busybox file system

漏洞复现----38、ThinkPHP5 5.0.23 远程代码执行漏洞

Alexnet of CNN classic network (Theory)

Only black-and-white box test is required for test opening post? No, but also learn performance test

每日面试1题-如何防止CDN防护被绕过

Deep understanding of JVM (III) - memory structure (III)

Deep understanding of JVM (IV) - garbage collection (I)
![The company was jailed for nonstandard bug during the test ~ [cartoon version]](/img/cd/42ab3fc0000fa7dfe2ac89de3486e4.jpg)
The company was jailed for nonstandard bug during the test ~ [cartoon version]

TCP session hijacking based on hunt1.5

同济、阿里的CVPR 2022最佳学生论文奖研究了什么?这是一作的解读
随机推荐
报名“互联网+”大赛华为云命题,诸多礼品任你拿!
Add code block in word (Reprint)
ASP. Net password encryption and password login
[Architecture] 1366- how to draw an excellent architecture diagram
DeFi借贷协议机制对比:Euler、Compound、Aave和Rari Capital
【云驻共创】Huawei iConnect使能物联终端一触即联
What does software testing need to learn? Test learning outline sorting
Mo Tianlun salon | Tsinghua qiaojialin: Apache iotdb, originated from Tsinghua, is building an open source ecological road
The new Post-00 Software Test Engineer in 2022 is a champion
Type ~ storage ~ variable in C #
Apache 解析漏洞(CVE-2017-15715)_漏洞复现
VS code 树视图 treeView
Deep understanding of JVM (III) - memory structure (III)
元宇宙带来的游戏变革会是怎样的?
ABAP-发布Restful服务
【二叉树】前序遍历构造二叉搜索树
How to write a technical proposal
Only black-and-white box test is required for test opening post? No, but also learn performance test
The company was jailed for nonstandard bug during the test ~ [cartoon version]
Post penetration file system + uploading and downloading files