当前位置:网站首页>力扣解法汇总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;
}
}边栏推荐
- Simulation of campus network design based on ENSP
- Deep understanding of JVM (II) - memory structure (II)
- 【剑指Offer】52. 两个链表的第一个公共节点
- Apache parsing vulnerability (cve-2017-15715)_ Vulnerability recurrence
- Cloud practice of key business migration of Internet of things by well-known Internet housing rental service companies
- Advanced Mathematics (Seventh Edition) Tongji University General exercises one person solution
- What will be the game changes brought about by the meta universe?
- The gates of Europe
- 基于SSM的新闻管理系统
- leetcode:787. The cheapest transfer flight in station K [k-step shortest path + DFS memory + defaultdict (dict)]
猜你喜欢

Spin lock exploration

墨天轮沙龙 | 清华乔嘉林:Apache IoTDB,源于清华,建设开源生态之路

Daily interview 1 question - how to prevent CDN protection from being bypassed
![[Netease Yunxin] playback demo build: unable to convert parameter 1 from](/img/6e/41e1eafd4c863c9e5f3a545b69a257.png)
[Netease Yunxin] playback demo build: unable to convert parameter 1 from "asyncmodalrunner *" to "std:: nullptr\u T"**

Canvas cloud shape animation

. Net ORM framework hisql practice - Chapter 1 - integrating hisql

Importing alicloud ECS locally to solve deployment problems

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

A tough battle for Tencent cloud

Six photos vous montrent pourquoi TCP serre la main trois fois?
随机推荐
Hyper-V: enable SR-IOV in virtual network
【二叉树】前序遍历构造二叉搜索树
Write the simplest small program in C language Hello World
生成对抗网络,从DCGAN到StyleGAN、pixel2pixel,人脸生成和图像翻译。
Develop those things: how to add text watermarks to videos?
5G商用三年,未来创新何去何从?
Exch:exchange server 2013 is about to end support
MSF后渗透总结
Small Tools(3) 集成Knife4j3.0.3接口文档
Development: how to install offline MySQL in Linux system?
AnimeSR:可学习的降质算子与新的真实世界动漫VSR数据集
Deep understanding of JVM (II) - memory structure (II)
Importing alicloud ECS locally to solve deployment problems
基于SSH的网上商城设计
What will be the game changes brought about by the meta universe?
leetcode:1042. Do not plant flowers adjacent to each other [randomly fill in qualified + no contradiction will be formed behind + set.pop]
元宇宙带来的游戏变革会是怎样的?
Apache parsing vulnerability (cve-2017-15715)_ Vulnerability recurrence
Generate confrontation network, from dcgan to stylegan, pixel2pixel, face generation and image translation.
[binary tree] preorder traversal to construct binary search tree