当前位置:网站首页>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
边栏推荐
- ASCII、Unicode、GBK、UTF-8之间的关系
- 用recyclerReview展示Banner,很简单
- Dx-11q signal relay
- About vctk datasets
- 【Qt5-基础篇_1】从0开始,德天老师和你一起学习——窗口简介
- Interpreting the scientific and technological literacy contained in maker Education
- Introduction and principle analysis of cluster and LVS
- Impact relay zc-23/dc220v
- 用 Flutter 的 Canvas 画点有趣的图形
- Green, green the reed. dew and frost gleam.
猜你喜欢

Double position relay dls-5/2 dc220v

Install redis database and download redis Desktop Manager in win11

Unknown database连接数据库错误

日志 logrus第三方库的使用

Two position relay st2-2l/ac220v

数字IC设计流程总结

Pre training / transfer learning of models

【学习笔记】倍增 + 二分

Locking relay ydb-100, 100V

Solve idea:class' xxx 'not found in module' xxx‘
随机推荐
visual studio 2019 快捷键备忘
mustache语法
Pre training / transfer learning of models
Locking relay ydb-100, 100V
ORB-SLAM2源码学习(二)地图初始化
【学习笔记】倍增 + 二分
1175. Prime Arrangements
Use strictmode strictmode principle (1)
Visual studio 2019 Download
用 Flutter 的 Canvas 画点有趣的图形
Analyze the maker education path integrating the essence of discipline
生意和投资的思考
MFC TCP communication server client demo notes vs2019
None of the following candidates is applicable because of a receiver type mismatch
[learning notes] structure
[leetcode] climb stairs [70]
gin_gorm
【动态规划】路径dp:931. Minimum Falling Path Sum
DLS-42/6-4 DC110V双位置继电器
元宇宙为 VR/AR 带来的新机会