当前位置:网站首页>Daily question: 1175 Prime permutation
Daily question: 1175 Prime permutation
2022-07-02 13:13:00 【Base-Case】
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 .
class Solution {
public:
bool vt[110];
int primes[110];
int numPrimeArrangements(int n) {
int mod=1e9+7;
int cnt=0;
// Sieve prime number
for(int i=2;i<=n;i++){
if(!vt[i]) primes[cnt++]=i;
for(int j=0;primes[j]<=n/i;j++){
vt[i*primes[j]]=true;
if(i%primes[j]==0) break;
}
}
long long sum=1;
// Prime total permutation
for(int i=2;i<=cnt;i++){
sum=(sum*i)%mod;
}
// Primes are all arranged
for(int i=n-cnt;i>1;i--){
sum=(sum*i)%mod;
}
return sum;
}
};边栏推荐
- Unity skframework framework (XX), VFX lab special effects library
- net share
- 研究表明“气味相投”更易成为朋友
- Get started REPORT | today, talk about the microservice architecture currently used by Tencent
- Std:: vector batch import fast de duplication method
- Rust language document Lite (Part 1) - cargo, output, basic syntax, data type, ownership, structure, enumeration and pattern matching
- 【蓝桥杯选拔赛真题43】Scratch航天飞行 少儿编程scratch蓝桥杯选拔赛真题讲解
- Five best software architecture patterns that architects must understand
- Ruby: how to copy variables without pointing to the same object- Ruby: how can I copy a variable without pointing to the same object?
- VIM super practical guide collection of this one is enough
猜你喜欢

Package management tools

Dijkstra AcWing 850. Dijkstra finding the shortest circuit II
![Jerry's watch gets the default ringtone selection list [article]](/img/94/e469864fa6ab688dabe46f606efdbc.jpg)
Jerry's watch gets the default ringtone selection list [article]

Unity SKFramework框架(十六)、Package Manager 开发工具包管理器

最近公共祖先LCA的三种求法

Counter attack of flour dregs: MySQL 66 questions, 20000 words + 50 pictures in detail! A little six

Js1day (syntaxe d'entrée / sortie, type de données, conversion de type de données, Var et let différenciés)

Unity skframework framework (XIX), POI points of interest / information points

Unity SKFramework框架(二十)、VFX Lab 特效库

Unity SKFramework框架(十四)、Extension 扩展函数
随机推荐
Js1day (input / output syntax, data type, data type conversion, VaR and let differences)
Js4day (DOM start: get DOM element content, modify element style, modify form element attributes, setinterval timer, carousel Map Case)
架构师必须了解的 5 种最佳软件架构模式
机器学习基础(二)——训练集和测试集的划分
js2day(又是i++和++i,if语句,三元运算符,switch、while语句,for循环语句)
How to get the operating system running PHP- How to get the OS on which PHP is running?
Linear DP acwing 898 Number triangle
8A Synchronous Step-Down regulator tps568230rjer_ Specification information
To bypass obregistercallbacks, you need to drive the signature method
Embedded software development
操作教程:EasyDSS如何将MP4点播文件转化成RTSP视频流?
How can attribute mapping of entity classes be without it?
Js3day (array operation, JS bubble sort, function, debug window, scope and scope chain, anonymous function, object, Math object)
Ltc3307ahv meets EMI standard, step-down converter qca7005-al33 phy
Unity SKFramework框架(十四)、Extension 扩展函数
Unity SKFramework框架(二十一)、Texture Filter 贴图资源筛选工具
Interesting interview questions
Linear DP acwing 895 Longest ascending subsequence
Independent and controllable 3D cloud CAD: crowncad enables innovative design of enterprises
[opencv learning] [image filtering]