当前位置:网站首页>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;
}
}边栏推荐
- C语言结构体
- Tencent cloud installs MySQL database
- Optimize with netcorebeauty Net core independent deployment directory structure
- K-line diagram interpretation and practical application skills (see position entry)
- VS code 树视图 treeView
- [machine learning] K-means clustering analysis
- ABAP publish restful service
- Apache 解析漏洞(CVE-2017-15715)_漏洞复现
- 联想“双平台”运维解决方案 助力智慧医疗行业智慧管理能力全面提升
- What did Tongji and Ali study in the CVPR 2022 best student thesis award? This is an interpretation of yizuo
猜你喜欢

Servlet operation principle_ API details_ Advanced path of request response construction (servlet_2)

腾讯持久化框架MMKV原理探究

Alexnet of CNN classic network (Theory)

ASP. Net password encryption and password login

Deep understanding of JVM (VI) -- garbage collection (III)

It's not easy to say I love you | use the minimum web API to upload files

Compile and generate busybox file system

If you want to learn software testing, you must see series, 2022 software testing engineer's career development

Redis (VII) - sentry

Mo Tianlun salon | Tsinghua qiaojialin: Apache iotdb, originated from Tsinghua, is building an open source ecological road
随机推荐
Redis (II) -- persistence
Spin lock exploration
MySQL reports that the column timestamp field cannot be null
Add code block in word (Reprint)
清华只能排第3?2022软科中国大学AI专业排名发布
ASP. Net generate verification code
Php8.0 environment detailed installation tutorial
ABAP-发布Restful服务
Small tools (3) integration knife4j3.0.3 interface document
每日面试1题-蓝队基础面试题-应急响应(1)应急响应基本思路流程+Windows入侵排查思路
Redis (IX) - enterprise level solution (II)
Simulation of campus network design based on ENSP
Ardunio esp32 obtains real-time temperature and humidity in mqtt protocol (DH11)
Only black-and-white box test is required for test opening post? No, but also learn performance test
ABAP publish restful service
【云驻共创】Huawei iConnect使能物联终端一触即联
Shortcut keys for the rainbow brackets plug-in
What did Tongji and Ali study in the CVPR 2022 best student thesis award? This is an interpretation of yizuo
MIT科技评论2022年35岁以下创新者名单发布,含AlphaFold作者等
Word中添加代码块(转载)