当前位置:网站首页>每日一题-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;
}
};
边栏推荐
- Basic usage, principle and details of session
- 解决:Thread 1:[<*>setValue:forUndefinedKey]:this class is not key value coding-compliant for the key *
- Take a cold bath
- 分布式锁的实现
- 【暑期每日一题】洛谷 P3742 umi的函数
- Solve the problem that the external chain file of Qiankun sub application cannot be obtained
- Dual contractual learning: text classification via label aware data augmentation reading notes
- LeetCode_ 28 (implement strstr())
- Sorting out 49 reports of knowledge map industry conference | AI sees the future with wisdom
- 线程类的几大创建方法
猜你喜欢

Use and modification of prior network model

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

Pytest automated testing - compare robotframework framework

PR 2021 quick start tutorial, learn about the and functions of the timeline panel

Pytorch(一) —— 基本语法

How to do the performance pressure test of "Health Code"

Pytorch(二) —— 激活函数、损失函数及其梯度
![[hard ten treasures] - 2 [basic knowledge] characteristics of various topological structures of switching power supply](/img/c2/6dfb9f477306edb46ff2a6a6ca32dd.png)
[hard ten treasures] - 2 [basic knowledge] characteristics of various topological structures of switching power supply
![[ue4] event distribution mechanism of reflective event distributor and active call event mechanism](/img/44/6a26ad24d56ddd5156f3a31fa7e0b9.jpg)
[ue4] event distribution mechanism of reflective event distributor and active call event mechanism

C#读写应用程序配置文件App.exe.config,并在界面上显示
随机推荐
Pytorch(一) —— 基本语法
Codeforces Round #771 (Div. 2) ABCD|E
Common UNIX Operation and maintenance commands of shell
科研狗可能需要的一些工具
【暑期每日一题】洛谷 P7222 [RC-04] 信息学竞赛
LeetCode_28(实现 strStr())
Collect the annual summary of laws, regulations, policies and plans related to trusted computing of large market points (national, ministerial, provincial and municipal)
线程安全问题
All in all, the low code still needs to solve these four problems
The index is invalid
Query long transaction
先有网络模型的使用及修改
Solve the problem that the external chain file of Qiankun sub application cannot be obtained
神经网络-卷积层
分布式事务-解决方案
How to view the changes and opportunities in the construction of smart cities?
2022 t elevator repair new version test questions and t elevator repair simulation test question bank
Some tools that research dogs may need
Section 27 remote access virtual private network workflow and experimental demonstration
[hardware ten treasures catalogue] - reprinted from "hardware 100000 whys" (under continuous update ~ ~)
