当前位置:网站首页>Daily question -leetcode1175- permutation of prime numbers - Mathematics
Daily question -leetcode1175- permutation of prime numbers - Mathematics
2022-07-01 04:53:00 【Li Fan, hurry up】
Note:
Prime numbers can be arranged blindly in the position of prime numbers , Non prime numbers can be arranged blindly in the position of non prime numbers , Each is a factorial arrangement
Find out 1 ~ n All prime numbers , Calculate factorials separately , The result multiplied together is the answer
Find the prime number and directly traverse it , Open a table to record whether it is a prime number at present , And how many primes are there to the current position
The code is as follows :
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;
}
};
边栏推荐
- FileInputStream
- Common UNIX Operation and maintenance commands of shell
- 【暑期每日一题】洛谷 P3742 umi的函数
- RuntimeError: “max_pool2d“ not implemented for ‘Long‘
- 解决:Thread 1:[<*>setValue:forUndefinedKey]:this class is not key value coding-compliant for the key *
- 每日一题-LeetCode1175-质数排列-数学
- This sideline workload is small, 10-15k, free unlimited massage
- 神经网络-卷积层
- Shell analysis server log command collection
- 1076 Forwards on Weibo
猜你喜欢

Pytorch(三) —— 函数优化

Common methods in transforms

STM32扩展板 温度传感器和温湿度传感器的使用

Data loading and preprocessing

Why is Internet thinking not suitable for AI products?

Dataloader的使用

pytorch中常用数据集的使用方法

Pytoch (I) -- basic grammar

RuntimeError: mean(): input dtype should be either floating point or complex dtypes.Got Long instead

Pytorch(二) —— 激活函数、损失函数及其梯度
随机推荐
[FTP] the solution to "227 entering passive mode" during FTP connection
Quelques outils dont les chiens scientifiques pourraient avoir besoin
对象的序列化与反序列化
分布式-总结列表
AssertionError assert I.ndim == 4 and I.shape[1] == 3
【暑期每日一题】洛谷 P1629 邮递员送信(未完待续...)
Technology sharing | broadcast function design in integrated dispatching
[hard ten treasures] - 1 [basic knowledge] classification of power supply
分布式锁的实现
RuntimeError: “max_pool2d“ not implemented for ‘Long‘
【暑期每日一题】洛谷 P7222 [RC-04] 信息学竞赛
[daily question in summer] Luogu p5740 [deep foundation 7. Example 9] the best student
[daily question in summer] Luogu p1568 race
【硬十宝典】——1.【基础知识】电源的分类
JS to solve the problem of floating point multiplication precision loss
Openresty rewrites the location of 302
Take a cold bath
洗个冷水澡吧
Solve the problem that the external chain file of Qiankun sub application cannot be obtained
Basic exercise of test questions hexadecimal to decimal
