当前位置:网站首页>力扣解法汇总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 - how to prevent CDN protection from being bypassed
- What should I pay attention to when playing futures? Where is safe to open an account? It's my first contact
- Fragmentary knowledge points of MySQL
- Alexnet of CNN classic network (Theory)
- 【二叉树】前序遍历构造二叉搜索树
- [machine learning] K-means clustering analysis
- Importing alicloud ECS locally to solve deployment problems
- Six pictures show you why TCP has three handshakes?
- Redis (IX) - enterprise level solution (II)
- 生成对抗网络,从DCGAN到StyleGAN、pixel2pixel,人脸生成和图像翻译。
猜你喜欢

Tubes响应性数据系统的设计与原理

K-line diagram interpretation and practical application skills (see position entry)

MIT science and Technology Review released the list of innovators under the age of 35 in 2022, including alphafold authors, etc

6 张图带你搞懂 TCP 为什么是三次握手?

Exploration and practice of "flow batch integration" in JD

Shortcut keys for the rainbow brackets plug-in

Redis (IV) - delete policy

Babbitt | yuanuniverse daily must read: minors ask for a refund after a reward. The virtual anchor says he is a big wrongdoer. How do you think of this regulatory loophole

Mo Tianlun salon | Tsinghua qiaojialin: Apache iotdb, originated from Tsinghua, is building an open source ecological road

New skill: accelerate node through code cache JS startup
随机推荐
Thinking on large file processing (upload, download)
China Infrastructure Development Association: electronic contract is recommended
How to write a technical proposal
Nielseniq welcomes dawn E. Norvell, head of retail lab, to accelerate the expansion of global retail strategy
Lenovo's "dual platform" operation and maintenance solution helps to comprehensively improve the intelligent management ability of the intelligent medical industry
[binary tree] preorder traversal to construct binary search tree
NFT铸造交易平台开发详情
如何写一个技术方案
News management system based on SSM
【剑指Offer】53 - I. 在排序数组中查找数字 I
元宇宙带来的游戏变革会是怎样的?
Simulation of campus network design based on ENSP
Redis (III) - transaction
6 張圖帶你搞懂 TCP 為什麼是三次握手?
Flutter custom component
送受伤婴儿紧急就医,滴滴司机连闯五个红灯
Advanced Mathematics (Seventh Edition) Tongji University General exercises one person solution
Spin lock exploration
Radio and television 5g officially set sail, attracting attention on how to apply the golden band
Fragmentary knowledge points of MySQL