当前位置:网站首页>[daily training] 1175 Prime permutation
[daily training] 1175 Prime permutation
2022-07-01 03:25:00 【Puppet__】
subject
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
Code
package dayLeetCode;
public class dayleetcode1175 {
int mod = 1000000007;
// Find how many prime numbers there are , Then arrange prime numbers and composite numbers
public int numPrimeArrangements(int n) {
int numPrimes = 0;
for (int i = 1; i <= n; i++){
if (isPrime(i)){
numPrimes++;
}
}
return (int)(f(numPrimes) % mod* f(n - numPrimes) % mod) % mod;
}
// Judge whether it is a prime number
private boolean isPrime(int k) {
if (k == 1){
return false;
}
for (int i = 2; i * i <= k; i++){
if (k % i == 0){
return false;
}
}
return true;
}
// Find the factorial
long f(int k){
long ans = 1;
for (int i = 1; i <= k; i++){
ans *= i;
ans %= mod;
}
return ans;
}
public static void main(String[] args) {
dayleetcode1175 obj = new dayleetcode1175();
System.out.println(obj.numPrimeArrangements(100));
}
}
边栏推荐
- 网页不能右键 F12 查看源代码解决方案
- Huawei operator level router configuration example | configuration static VPLS example
- 限流组件设计实战
- Introduction to the core functions of webrtc -- an article to understand peerconnectionfactoryinterface rtcconfiguration peerconnectioninterface
- Chapitre 03 Bar _ Gestion des utilisateurs et des droits
- Error accessing URL 404
- Best used trust automation script (shell)
- Hello World generation
- 完全背包问题
- Listener listener
猜你喜欢
随机推荐
Golang多图生成gif
Cloud native annual technology inventory is released! Ride the wind and waves at the right time
Kmeans
So easy 将程序部署到服务器
Listener listener
访问url 404 的错误
Completely solve the lost connection to MySQL server at 'reading initial communication packet
【日常训练】1175. 质数排列
pytest-fixture
How to use hybrid format to output ISO files? isohybrid:command not found
XXL job User Guide
Metadata in NFT
Keil5中如何做到 0 Error(s), 0 Warning(s).
Depth first traversal of C implementation Diagram -- non recursive code
Learning notes for introduction to C language multithreaded programming
第03章_用户与权限管理
后台系统页面左边菜单按钮和右边内容的处理,后台系统页面出现双滚动
Druid监控统计数据源
数组的includes( )
How do spark tasks of 10W workers run? (Distributed Computing)