当前位置:网站首页>[1175. prime number arrangement]
[1175. prime number arrangement]
2022-06-30 20:31:00 【Sugar_ wolf】
source : Power button (LeetCode)
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
Method : Prime number judgment + Combinatorial mathematics
Ideas
Find the number of schemes that meet the conditions , So that all prime numbers are placed on the prime index , All composite numbers are placed on the composite index , Prime number placement and composite number placement are independent of each other , The total number of schemes is 「 The number of schemes in which all prime numbers are placed on the prime index 」 ×「 The number of schemes in which all composite numbers are placed on the composite index 」. seek 「 The number of schemes in which all prime numbers are placed on the prime index 」, That is, find the number of prime numbers numPrimes The factorial .「 The number of schemes in which all composite numbers are placed on the composite index 」 Empathy . When finding the number of primes , You can use trial division .
Code :
const int MOD = 1e9 + 7;
class Solution {
public:
int numPrimeArrangements(int n) {
int numPrimes = 0;
for (int i = 1; i <= n; i++) {
if (isPrime(i)) {
numPrimes++;
}
}
return (int) (factorial(numPrimes) * factorial(n - numPrimes) % MOD);
}
bool isPrime(int n) {
if (n == 1) {
return false;
}
for (int i = 2; i * i <= n; i++) {
if (n % i == 0) {
return false;
}
}
return true;
}
long factorial(int n) {
long res = 1;
for (int i = 1; i <= n; i++) {
res *= i;
res %= MOD;
}
return res;
}
};
Execution time :0 ms, In all C++ Defeated in submission 100.00% Users of
Memory consumption :5.9 MB, In all C++ Defeated in submission 45.81% Users of
author:LeetCode-Solution
边栏推荐
- 请问海量数据如何去取最大的K个
- 开会,OneMeeting,OK!
- 杰理之用测试盒配对软件修改注意点【篇】
- Jenkins打包拉取不到最新的jar包
- mysql登录出现1045错误修改方法[通俗易懂]
- [iccv 2019] characteristics precise supervision of feature super resolution for small object detection
- Big God explains open source buff gain strategy live broadcast
- Is it safe to open an account for online stock trading!?
- 最新海康摄像机、NVR、流媒体服务器、回放取流RTSP地址规则说明[通俗易懂]
- Heartbeat and DRBD configuration process
猜你喜欢

Summary of operating system interview questions (updated from time to time)

Maya house modeling

Torchdrug -- drug attribute prediction

屏幕显示技术进化史

基于开源流批一体数据同步引擎ChunJun数据还原—DDL解析模块的实战分享
![Jerry's touch key recognition process [chapter]](/img/ec/25d2d6fd26571e4fb642129a4eee1b.png)
Jerry's touch key recognition process [chapter]

杰理之触摸按键识别流程【篇】

PostgreSQL heap堆表 存储引擎实现原理

How to pass the PMP Exam quickly?

Halcon知识:盘点一下计量对象【1】
随机推荐
What is the difference between tolocal8bit and toutf8() in QT
pytorch实现FLOPs和Params的计算
好高的佣金,《新程序员》合伙人计划来袭,人人皆可参与
北京大学ACM Problems 1005:I Think I Need a Houseboat
maya房子建模
大神詳解開源 BUFF 增益攻略丨直播
Network planning | [five transport layers and six application layers] knowledge points and examples
Taihu Lake "China's healthy agricultural products · mobile phone live broadcast" enters Taihu Lake
Meeting, onemeeting, OK!
maya房子建模
Jenkins can't pull the latest jar package
黑苹果 服务器系统安装教程,黑苹果安装教程,详细教您黑苹果怎么安装[通俗易懂]
STL的基本组成部分
Exness: liquidity series - liquidity cleaning and reversal, decision interval
Jerry's touch key recognition process [chapter]
SQL优化
杰理之检测灵敏度级别确定【篇】
All the important spark summit features were released here last night (with ultra clear video attached)
项目经理面试常见问题及回答技巧
C语言:hashTable
