当前位置:网站首页>力扣解法汇总1175-质数排列
力扣解法汇总1175-质数排列
2022-06-30 16:41:00 【失落夏天】
目录链接:
力扣编程题-解法汇总_分享+记录-CSDN博客
GitHub同步刷题项目:
https://github.com/September26/java-algorithms
原题链接:力扣
描述:
请你帮忙给从 1 到 n 的数设计排列方案,使得所有的「质数」都应该被放在「质数索引」(索引从 1 开始)上;你需要返回可能的方案总数。
让我们一起来回顾一下「质数」:质数一定是大于 1 的,并且不能用两个小于它的正整数的乘积来表示。
由于答案可能会很大,所以请你返回答案 模 mod 10^9 + 7 之后的结果即可。
示例 1:
输入:n = 5
输出:12
解释:举个例子,[1,2,5,4,3] 是一个有效的排列,但 [5,2,3,4,1] 不是,因为在第二种情况里质数 5 被错误地放在索引为 1 的位置上。
示例 2:
输入:n = 100
输出:682289015
提示:
1 <= n <= 100
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/prime-arrangements
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
解题思路:
* 解题思路: * 求出质数和非质数的数量,然后分别排序。乘积就是方案总数。
代码:
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;
}
//取模运算
public static long ramainder(long dividend, long dividor) {
return dividend % dividor;
}
}
边栏推荐
- Daily interview 1 question - basic interview question of blue team - emergency response (1) basic idea process of emergency response +windows intrusion screening idea
- Add code block in word (Reprint)
- 自旋锁探秘
- 【义修换届大礼包】
- MySQL reports that the column timestamp field cannot be null
- 【网易云信】播放demo构建:无法将参数 1 从“AsyncModalRunner *”转换为“std::nullptr_t”**
- Fragmentary knowledge points of MySQL
- Send the injured baby for emergency medical treatment. Didi's driver ran five red lights in a row
- 【机器学习】K-means聚类分析
- Exch:exchange server 2013 is about to end support
猜你喜欢
Redis (IX) - enterprise level solution (II)
Deep understanding of JVM (I) - memory structure (I)
Building a basic buildreoot file system
Solution: STM32 failed to parse data using cjson
Add code block in word (Reprint)
leetcode:787. The cheapest transfer flight in station K [k-step shortest path + DFS memory + defaultdict (dict)]
如何写一个技术方案
Six photos vous montrent pourquoi TCP serre la main trois fois?
Hyper-V: enable SR-IOV in virtual network
Deep understanding of JVM (IV) - garbage collection (I)
随机推荐
每日面试1题-如何防止CDN防护被绕过
Six pictures show you why TCP has three handshakes?
Tencent cloud installs MySQL database
6 张图带你搞懂 TCP 为什么是三次握手?
Generate confrontation network, from dcgan to stylegan, pixel2pixel, face generation and image translation.
港科大&MSRA新研究:关于图像到图像转换,Finetuning is all you need
A tough battle for Tencent cloud
Mo Tianlun salon | Tsinghua qiaojialin: Apache iotdb, originated from Tsinghua, is building an open source ecological road
Simulation of campus network design based on ENSP
Apache parsing vulnerability (cve-2017-15715)_ Vulnerability recurrence
Building a basic buildreoot file system
基于SSH的网上商城设计
Several points in MySQL that are easy to ignore and forget
Exch: repair the missing system mailbox
Redis (VI) - master-slave replication
IEEE TBD SCI impact factor increased to 4.271, ranking Q1!
Inventory in the first half of 2022: summary of major updates and technical points of 20+ mainstream databases
splitting. JS password display hidden JS effect
. Net ORM framework hisql practice - Chapter 1 - integrating hisql
Small Tools(3) 集成Knife4j3.0.3接口文档