当前位置:网站首页>1175. Prime Arrangements
1175. Prime Arrangements
2022-07-01 01:29: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.
source : Power button (LeetCode)
link :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.
source : Power button (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
边栏推荐
猜你喜欢

ESP8266 RC522

友盟(软件异常实时监听的好帮手:Crash)接入教程(有点基础的小白最易学的教程)

软件开发完整流程

Training discipline principle of robot programming

K210 site helmet

Principes de formation de la programmation robotique

机器人编程的培训学科类原理

双位置继电器ST2-2L/AC220V

Looksrare team's "cash out" caused disturbance

Install redis database and download redis Desktop Manager in win11
随机推荐
gin 配置文件
ORB-SLAM2源码学习(二)地图初始化
Looksrare team's "cash out" caused disturbance
【go】go 实现行专列 将集合进行转列
DLS-42/6-4 DC110V双位置继电器
Interpreting the scientific and technological literacy contained in maker Education
Green, green the reed. dew and frost gleam.
【栈】921. Minimum Add to Make Parentheses Valid
【学习笔记】倍增 + 二分
Pre training / transfer learning of models
流批一体在京东的探索与实践
使用 C# 创造 ASCII 艺术
Using recyclerreview to show banner is very simple
Parity linked list [two general directions of linked list operation]
Open3D 点云颜色渲染
Double position relay dls-5/2 dc220v
uniapp官方组件点击item无效,解决方案
【office办公-pdf篇】pdf合并与拆分让我们摆脱付费软件的功能限制好不好
[network packet loss and network delay? This artifact can help you deal with everything!]
解读创客教育所蕴含的科技素养