当前位置:网站首页>[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
边栏推荐
猜你喜欢

SQL优化

昨晚 Spark Summit 重要功能发布全在这里(附超清视频)

Cv+deep learning network architecture pytoch recurrence series basenets (backbones) (I)

漏洞扫描工具大全,妈妈再也不用担心我挖不到漏洞了

Why must we move from Devops to bizdevops?

好高的佣金,《新程序员》合伙人计划来袭,人人皆可参与

Maya House Modeling

CADD课程学习(1)-- 药物设计基础知识

How to pass the PMP Exam quickly?

STL的基本组成部分
随机推荐
网上炒股开户安全嘛!?
To eliminate bugs, developers must know several bug exploration and testing artifacts.
CADD course learning (1) -- basic knowledge of drug design
微信小程序开发实战 云音乐
Meeting, onemeeting, OK!
Informatics Olympiad 1362: family problems
Heartbeat 与DRBD 配置过程
以全栈全功能解决方案,应对多样工具复杂环境DevOps落地难题
亚马逊在阿拉伯联合酋长国限制LGBTQ相关的搜索和产品销售
VB的基本语法
Document contains & conditional competition
Description of the latest RTSP address rules for Hikvision camera, NVR, streaming media server, playback and streaming [easy to understand]
Qt和其它GUI库的对比
[try to hack] windows system account security
Exness: liquidity series - liquidity cleaning and reversal, decision interval
Build document editor based on slate
unittest自动测试多个用例时,logging模块重复打印解决
Summary of PHP file upload (garbled code, move failure, permission, display picture)
北京大学ACM Problems 1006:Biorhythms
左值引用和右值引用
