当前位置:网站首页>1175. 质数排列 / 剑指 Offer II 104. 排列的数目
1175. 质数排列 / 剑指 Offer II 104. 排列的数目
2022-06-30 23:12:00 【彼淇梁】
1175. 质数排列【简单题】【每日一题】
思路:
求出质数的个数k1,n-k1求出非质数个数k2,则符合要求的种类个数是 k1的阶乘 * k2的阶乘,由于运算结果可能越界,因此根据题意对运算结果取10^9+7模。
设 MOD = 10^9+7,因此上述运算结果可表示为:
ans = (k1! * k2!) % MOD
由于阶乘运算过程中也可能越界,因此对上式进行数学变换
ans = (k1! * k2!) % MOD
= ((k1! % MOD) * (K2! % MOD)) % MOD
//【】表示累乘
= ((【k1i % MOD】 % MOD ) * (【k2i % MOD】 % MOD ) ) % MOD
= (【k1i % MOD】 * 【k2i % MOD】) % MOD
代码:
class Solution {
static int MOD = (int) 1e9+7;
public int numPrimeArrangements(int n) {
if (n <= 2){
return 1;
}
int k1 = 1;
for (int i = 3; i <= n; i++) {
if (isPrime(i)){
k1++;
}
}
int k2 = n - k1;
long ans = 1;
for (int i = 2; i <= k1; i++) {
ans = ans * i % MOD;
}
for (int i = 2; i <= k2; i++) {
ans = ans * i % MOD;
}
return (int) (ans);
}
public boolean isPrime(int n){
int k = (int) Math.sqrt(n);
for (int i = 2; i <= k ; i++) {
if (n % i == 0){
return false;
}
}
return true;
}
}
剑指 Offer II 104. 排列的数目【中等题】
思路:【动态规划】
被昨天背的完全背包公式给骗了~,这个题不是背包问题
代码:
class Solution {
public int combinationSum4(int[] nums, int target) {
//定义dp数组,dp[i]表示和为i的组合数
int[] dp = new int[target+1];
//边界条件 i = 0时,和为0的组合只有一种情况就是 不选任何元素,因此dp[0] = 1
dp[0] = 1;
//转移方程
for (int i = 1; i <= target; i++) {
for (int num : nums) {
//遍历nums数组,如果目标和i大于等于当前元素值num,说明这个元素可以被选用dp[i]的值等于数组中所有满足i>=num的dp值之和
if (i >= num) {
dp[i] += dp[i - num];
}
}
}
return dp[target];
}
}
边栏推荐
- CTFSHOW框架复现篇
- Prospects of world digitalization and machine intelligence in the next decade
- 在线客服聊天系统源码_美观强大golang内核开发_二进制运行傻瓜式安装_附搭建教程...
- 严格次小生成树
- [fundamentals of wireless communication-13]: illustrated mobile communication technology and application development-1-overview
- Cas classique multithreadé
- Fund clients and sales agencies
- AtCoder Beginner Contest 257
- As the public cloud market enters the deep water, can the calm Amazon cloud still sit still?
- What does the &?
猜你喜欢

5g smart building solution 2021
![[Android, kotlin, tflite] mobile device integration deep learning light model tflite (object detection)](/img/7e/3e6ebfb90a82249d934296a041ba21.png)
[Android, kotlin, tflite] mobile device integration deep learning light model tflite (object detection)

Introduction to machine learning compilation course learning notes lesson 2 tensor program abstraction

Redis' cache penetration, cache breakdown and cache avalanche

多线程经典案例
![[fundamentals of wireless communication-13]: illustrated mobile communication technology and application development-1-overview](/img/1d/62e55f1b5445d7349ec383879f4275.png)
[fundamentals of wireless communication-13]: illustrated mobile communication technology and application development-1-overview

ESP8266 成为客户端和服务器

Achieve secure data sharing among multiple parties and solve the problem of asymmetric information in Inclusive Finance

Redis的事务和锁机制

Doker的容器数据卷
随机推荐
企业出海数字化转型解决方案介绍
Cesiumjs 2022 ^ source code interpretation [6] - new architecture of modelempirical
如何区分平台安全和网上炒作?网络投机有哪些止损技巧?
35家巨头科技公司联合组成元宇宙标准论坛组织
Fund sales code of conduct and information management
flutter - sort List排序
Redis' cache penetration, cache breakdown and cache avalanche
Ctfshow permission maintenance
Neo4j load CSV configuration and use
Ideal interface automation project
【Android,Kotlin,TFLite】移动设备集成深度学习轻模型TFlite(物体检测篇)
[无线通信基础-13]:图解移动通信技术与应用发展-1-概述
Introduction to machine learning compilation course learning notes lesson 2 tensor program abstraction
后疫情时代,云计算如何为在线教育保驾护航
The sandbox is being deployed on the polygon network
Lombok
206 page Shanghai BIM Technology Application and development report 2021
One revolution, two forces and three links: the "carbon reduction" road map behind the industrial energy efficiency improvement action plan
Wechat applet transmits parameters (data-) by clicking events
多線程經典案例