当前位置:网站首页>1175. Prime Arrangements
1175. Prime Arrangements
2022-07-01 00:40:00 【SUNNY_CHANGQI】
The description of the problem
Return the number of permutations of 1 to n so that prime numbers are at prime indices (1-indexed.)
(Recall that an integer is prime if and only if it is greater than 1, and cannot be written as a product of two positive integers both smaller than it.)
Since the answer may be large, return the answer modulo 10^9 + 7.
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/prime-arrangements
an example
Input: n = 5
Output: 12
Explanation: For example [1,2,5,4,3] is a valid permutation, but [5,2,3,4,1] is not because the prime number 5 is at index 1.
来源:力扣(LeetCode)
Notification
The overflow problem (mode and long to address this problem)
The codes for this
#include <cmath>
#include <iostream>
#include <vector>
using namespace std;
class Solution {
public:
const int MOD = 1e9 + 7;
int numPrimeArrangements(int n) {
int primeNum = 0, res;
primeNum = getPrimeNum(n);
res = getRes(primeNum, n);
return res;
}
int getPrimeNum(int n) {
int primeNum = 0;
for (int i = 1; i <= n; i++) {
if (isPrime(i)) {
primeNum++;
}
}
return primeNum;
}
bool isPrime(int n) {
if (n == 1) {
return false;
}
for (auto i = 2; i * i <= n; ++i) {
if (n % i == 0) {
return false;
}
}
return true;
}
int getRes(int primeNum, int n) {
long res = 1;
for (int i = 1; i <= primeNum; i++) {
res = (res * i) % MOD;
}
for (int i = 1; i <= n - primeNum; i++) {
res = (res * i) % MOD;
}
return (int)res;
}
};
int main() {
Solution s;
long long unsigned int n = 5;
cout << s.numPrimeArrangements(n) << endl;
return 0;
}
The corresponding results
$ ./test
12
边栏推荐
- Double linked list: initialize insert delete traversal
- 奇偶链表[链表操作的两种大方向]
- Shift operators
- js中把数字转换成汉字输出
- 软硬件基础知识学习--小日记(1)
- 友盟(软件异常实时监听的好帮手:Crash)接入教程(有点基础的小白最易学的教程)
- Fluent JSON serialization deserialization
- Web interface testing of software testing
- ESP8266 RC522
- ArrayList analysis 1-cycle, capacity expansion, version
猜你喜欢

Golang treasure house recommendation

集群与LVS介绍及原理解析

Exploring the road of steam education innovation in the Internet Era

【go】go 实现行专列 将集合进行转列

Installing mongodb database in Windows Environment
![[learning notes] double + two points](/img/d4/1ef449e3ef326a91966da11b3c8210.png)
[learning notes] double + two points

Pytorch installs and uses GPU acceleration
![分割链表[先取next再斩断链表防止断链]](/img/eb/708ab20c13df75f4dbd2d6461d3602.png)
分割链表[先取next再斩断链表防止断链]

冲击继电器ZC-23/DC220V

Exercises on recursion in C language
随机推荐
Oracle table creation and management
Dx-11q signal relay
二十多年来第一次!CVPR最佳学生论文授予中国高校学生!
The girlfriend said: if you want to understand the three MySQL logs, I will let you heiheihei!
Two-stage RO: part 1
Two position relay st2-2l/ac220v
K210门禁毕设
【go】go 实现行专列 将集合进行转列
双位置继电器ST2-2L/AC220V
机器人编程的培训学科类原理
Green, green the reed. dew and frost gleam.
Vnctf 2022 cm CM1 re reproduction
解析融合学科本质的创客教育路径
Golang treasure house recommendation
染色法判断二分图
关于VCTK数据集
文件服务设计
Implementation of date class
K210 site helmet
ArrayList analysis 1-cycle, capacity expansion, version