当前位置:网站首页>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
边栏推荐
- What if the disk of datanode is full?
- 问题解决:如何管理线程私有(thread_local)的指针变量
- 文件服务设计
- What is product thinking
- Detailed analysis of operators i++ and ++i in JS, i++ and ++i
- 解析融合学科本质的创客教育路径
- [LeetCode] 两数之和【1】
- 2022 is half way through. It's hard to make money
- For the first time in more than 20 years! CVPR best student thesis awarded to Chinese college students!
- Web compatibility testing of software testing
猜你喜欢

机器人编程的培训学科类原理
![分割链表[先取next再斩断链表防止断链]](/img/eb/708ab20c13df75f4dbd2d6461d3602.png)
分割链表[先取next再斩断链表防止断链]

PHP online confusion encryption tutorial sharing + basically no solution

For the first time in more than 20 years! CVPR best student thesis awarded to Chinese college students!

New content violation degree determination scana bad information monitoring capability update issue 5

HDU 2488 A Knight's Journey(DFS)

用recyclerReview展示Banner,很简单

Green, green the reed. dew and frost gleam.

解析创客教育实践中的智慧原理

【学习笔记】倍增 + 二分
随机推荐
High quality pump SolidWorks model material recommended, not to be missed
pull_ to_ refresh
解决IDEA:Class ‘XXX‘ not found in module ‘XXX‘
【原创】 PLSQL 索引排序优化
Kongyiji's first question: how much do you know about service communication?
Using C language to realize the exchange between the contents of two arrays (provided that the array is the same size)
The longest selling mobile phone in China has been selling well since its launch, crushing iphone12
Training discipline principle of robot programming
[learning notes] double + two points
The communication mechanism and extension of Supervisor
用recyclerReview展示Banner,很简单
ESP8266 RC522
C语言一点点(未来可会增加)
Tcp/ip protocol stack, about TCP_ RST | TCP_ ACK correct attitude
mustache语法
Service
问题解决:如何管理线程私有(thread_local)的指针变量
二十多年来第一次!CVPR最佳学生论文授予中国高校学生!
染色法判断二分图
分割链表[先取next再斩断链表防止断链]