当前位置:网站首页>每日一题-LeetCode1175-质数排列-数学
每日一题-LeetCode1175-质数排列-数学
2022-07-01 04:48:00 【李烦烦搞快点】
Note:
质数可以在质数的位置上瞎排,非质数可以在非质数的位置上瞎排,每个都是阶乘的排法
求出来1 ~ n所有的质数,分别算个阶乘,结果乘到一起就是答案
求质数就直接遍历就行了,开个表记录下来当前是不是质数,以及到当前位置质数有多少个
代码如下:
class Solution {
public:
int mod = 1e9 + 7;
int numPrimeArrangements(int n) {
vector<int> list;
int cnt[110];
for(int i = 2; i <= n; i ++){
bool isPrime = true;
for(int j = 2; j * j <= i; j ++){
if(i % j == 0) isPrime = false;
}
if(isPrime) list.push_back(i);
cnt[i] = list.size();
}
long long ans = 1, a = cnt[n], b = n - a;
for(int i = a; i > 1; i --) ans = ans * i % mod;
for(int i = b; i > 1; i --) ans = ans * i % mod;
return (int)ans;
}
};
边栏推荐
- C#读写应用程序配置文件App.exe.config,并在界面上显示
- Neural network - nonlinear activation
- 【暑期每日一题】洛谷 P1629 邮递员送信(未完待续...)
- Neural networks - use of maximum pooling
- 手动实现一个简单的栈
- STM32扩展板 数码管显示
- 神经网络-最大池化的使用
- Fitness without equipment
- 2022 G2 power station boiler stoker examination question bank and G2 power station boiler stoker simulation examination question bank
- 【暑期每日一题】洛谷 P2637 第一次,第二次,成交!
猜你喜欢
分布式事务-解决方案
pytorch中常用数据集的使用方法
Pytest automated testing - compare robotframework framework
VIM easy to use tutorial
Pytorch(三) —— 函数优化
Neural network - nonlinear activation
PR 2021 quick start tutorial, learn about the and functions of the timeline panel
C - detailed explanation of operators and summary of use cases
先有网络模型的使用及修改
Kodori tree board
随机推荐
【硬十宝典目录】——转载自“硬件十万个为什么”(持续更新中~~)
Pytorch convolution operation
STM32扩展板 温度传感器和温湿度传感器的使用
Codeworks round 449 (Div. 1) C. Kodori tree template
FileInputStream
RuntimeError: “max_pool2d“ not implemented for ‘Long‘
VIM简易使用教程
[daily question in summer] first time, second time, deal!
Basic exercise of test questions hexadecimal to decimal
神经网络-非线性激活
Sorting out 49 reports of knowledge map industry conference | AI sees the future with wisdom
洗个冷水澡吧
About the transmission pipeline of stage in spark
神经网络的基本骨架-nn.Moudle的使用
RuntimeError: mean(): input dtype should be either floating point or complex dtypes. Got Long instead
Dual contractual learning: text classification via label aware data augmentation reading notes
Announcement on the list of Guangdong famous high-tech products to be selected in 2021
先有网络模型的使用及修改
神经网络-使用Sequential搭建神经网络
Shell之分析服务器日志命令集锦