当前位置:网站首页>[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
边栏推荐
- Great God detailed open source Buff gain Introduction 丨 Live
- 北京大学ACM Problems 1001:Exponentiation
- 网上炒股开户安全嘛!?
- 杰理之触摸按键识别流程【篇】
- PHP文件上传小结(乱码,移动失败,权限,显示图片)
- Jerry's long press reset [chapter]
- NLP技能树学习路线-(一)路线总览
- 请问海量数据如何去取最大的K个
- By analyzing more than 7million R & D needs, it is found that these eight programming languages are the most needed by the industry
- DEX file parsing - Method_ IDS resolution
猜你喜欢

Lambda 表达式原理分析学习(2022.06.23)

Why must we move from Devops to bizdevops?

为什么一定要从DevOps走向BizDevOps?

如何快速通过PMP考试?

大神详解开源 BUFF 增益攻略丨直播

NLP paper lead reading | what about the degradation of text generation model? Simctg tells you the answer

The Commission is so high that everyone can participate in the new programmer's partner plan

Big God explains open source buff gain strategy live broadcast

Taihu Lake "China's healthy agricultural products · mobile phone live broadcast" enters Taihu Lake
![25: Chapter 3: developing pass service: 8: [registration / login] interface: receiving and verifying](/img/ff/727c4a20ff3816ec7221dced5a4770.png)
25: Chapter 3: developing pass service: 8: [registration / login] interface: receiving and verifying "mobile number and verification code" parameters; (it is important to know the application scenario
随机推荐
Jerry's touch key recognition process [chapter]
exness:流动性系列-流动性清洗和反转、决策区间
请问海量数据如何去取最大的K个
谈谈内联函数
昨晚 Spark Summit 重要功能发布全在这里(附超清视频)
Basic syntax of VB
Is the project manager a leader? Can you criticize and blame members?
CADD课程学习(2)-- 靶点晶体结构信息
The Commission is so high that everyone can participate in the new programmer's partner plan
Meeting, onemeeting, OK!
DEX文件解析 - method_ids解析
亚马逊在阿拉伯联合酋长国限制LGBTQ相关的搜索和产品销售
杰理之关于长按开机检测抬起问题【篇】
Filebeat custom indexes and fields
MySQL master-slave synchronization
PM这样汇报工作,老板心甘情愿给你加薪
Pytorch implements the calculation of flops and params
[ICLR 2021] semi supervised object detection: unbiased teacher for semi supervised object detection
How unity pulls one of multiple components
Encoding type of Perl conversion file
