当前位置:网站首页>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
边栏推荐
猜你喜欢

Solve idea:class' xxx 'not found in module' xxx‘

flutter报错 -- The argument type ‘Function‘ can‘t be assigned to the parameter type ‘void Function()?‘

Opencv basic operation 2 realizes label2rgb and converts gray-scale images into color images

Analyzing the wisdom principle in maker education practice

孙宇晨接受瑞士媒体Bilan采访:熊市不会持续太久

Docker deployment MySQL 8
![[network packet loss and network delay? This artifact can help you deal with everything!]](/img/c4/f733b23327458b9266b9cbcccb6f14.png)
[network packet loss and network delay? This artifact can help you deal with everything!]
![[问题已处理]-nvidia-smi命令获取不到自身容器的GPU进程和外部的GPU进程号](/img/51/e48e222c14f4a4e9f2be91a677033f.png)
[问题已处理]-nvidia-smi命令获取不到自身容器的GPU进程和外部的GPU进程号
![Parity linked list [two general directions of linked list operation]](/img/4e/ce860bc172bb75f456427ba26a7842.png)
Parity linked list [two general directions of linked list operation]

基础知识之二——STA相关的基本定义
随机推荐
JS to convert numbers into Chinese characters for output
Introduction and principle analysis of cluster and LVS
DC學習筆記正式篇之零——綜述與基本流程介紹
mysql数据库基础:流程控制
Split the linked list [take next first and then cut the linked list to prevent chain breakage]
Why build a personal blog
奇偶链表[链表操作的两种大方向]
StrictMode分析Activity泄漏-StrictMode原理(3)
Dls-20 double position relay 220VDC
机器人编程的培训学科类原理
用Steam教育启发学生多元化思维
(学习力+思考力) x 行动力,技术人成长的飞轮效应总结
Basic knowledge of software and hardware -- diary (1)
[leetcode] climb stairs [70]
【队列】933. Number of Recent Calls
Visual studio 2019 shortcut notes
fluttertoast
OCR的一些项目
解析创客教育实践中的智慧原理
生意和投资的思考