当前位置:网站首页>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;
}
}边栏推荐
- MIT science and Technology Review released the list of innovators under the age of 35 in 2022, including alphafold authors, etc
- Elastic 8.0: opening a new era of speed, scale, relevance and simplicity
- 基于SSH的客户关系CRM管理系统
- ASP. Net generate verification code
- Small Tools(3) 集成Knife4j3.0.3接口文档
- Optimization of interface display for general kernel upgrade of mobo video management system v3.5.0
- [Netease Yunxin] playback demo build: unable to convert parameter 1 from "asyncmodalrunner *" to "std:: nullptr\u T"**
- Deep understanding of JVM (IV) - garbage collection (I)
- Development details of NFT casting trading platform
- 墨天轮沙龙 | 清华乔嘉林:Apache IoTDB,源于清华,建设开源生态之路
猜你喜欢

Flutter custom component

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

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

Simulation of campus network design based on ENSP

Optimize with netcorebeauty Net core independent deployment directory structure

The new Post-00 Software Test Engineer in 2022 is a champion

巴比特 | 元宇宙每日必读:未成年人打赏后要求退款,虚拟主播称自己是大冤种,怎么看待这个监管漏洞?...
![[zero basic IOT pwn] environment construction](/img/3b/a0689a1570fcc40bb9a5a4e9cdc63c.png)
[zero basic IOT pwn] environment construction
![leetcode:787. The cheapest transfer flight in station K [k-step shortest path + DFS memory + defaultdict (dict)]](/img/28/78e2961877776ca3dfcba5ee7e35d2.png)
leetcode:787. The cheapest transfer flight in station K [k-step shortest path + DFS memory + defaultdict (dict)]

零基础也能做Apple大片!这款免费工具帮你渲染、做特效、丝滑展示
随机推荐
Post office - post office issues (dynamic planning)
MySQL advanced - index optimization (super detailed)
Tubes响应性数据系统的设计与原理
Post penetration file system + uploading and downloading files
每日面试1题-蓝队基础面试题-应急响应(1)应急响应基本思路流程+Windows入侵排查思路
NFT铸造交易平台开发详情
Post MSF infiltration summary
Shortcut keys for the rainbow brackets plug-in
Grep output with multiple colors- Grep output with multiple Colors?
巴比特 | 元宇宙每日必读:未成年人打赏后要求退款,虚拟主播称自己是大冤种,怎么看待这个监管漏洞?...
Animesr: learnable degradation operator and new real world animation VSR dataset
ASP. Net generate verification code
[binary tree] preorder traversal to construct binary search tree
Design of online shopping mall based on SSH
Optimize with netcorebeauty Net core independent deployment directory structure
DeFi借贷协议机制对比:Euler、Compound、Aave和Rari Capital
元宇宙带来的游戏变革会是怎样的?
Redis (I) - data type
Solve the problem of unable to connect to command metric stream and related problems in the hystrix dashboard
Tencent cloud installs MySQL database