当前位置:网站首页>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
边栏推荐
- Exercise and health
- JS方法大全的一个小文档
- C语言一点点(未来可会增加)
- Technical personnel advanced to draw a big picture of business, hand-in-hand teaching is coming
- Green, green the reed. dew and frost gleam.
- CMU15445 (Fall 2019) 之 Project#1 - Buffer Pool 详解
- 问题解决:如何管理线程私有(thread_local)的指针变量
- Flutter Error: Cannot run with sound null safety, because the following dependencies don‘t support
- 染色法判断二分图
- 友盟(软件异常实时监听的好帮手:Crash)接入教程(有点基础的小白最易学的教程)
猜你喜欢
随机推荐
Dx-11q signal relay
The real topic of the 11th provincial competition of Bluebridge cup 2020 - crop hybridization
Parity linked list [two general directions of linked list operation]
Web interface testing of software testing
Double linked list: initialize insert delete traversal
Analysis of blocktoken principle
对libco的一点看法
Technical personnel advanced to draw a big picture of business, hand-in-hand teaching is coming
解析融合学科本质的创客教育路径
ASCII、Unicode、GBK、UTF-8之间的关系
2021电赛F题openmv和K210调用openmv api巡线,完全开源。
The quantity and quality of the devil's cold rice 101; Employee management; College entrance examination voluntary filling; Game architecture design
機器人編程的培訓學科類原理
解决IDEA:Class ‘XXX‘ not found in module ‘XXX‘
Analyzing the wisdom principle in maker education practice
XJY-220/43AC220V静态信号继电器
闭锁继电器YDB-100、100V
冲击继电器ZC-23/DC220V
人穷志不短,穷学生也能玩转树莓派
P4 learning - p4runtime









