当前位置:网站首页>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];
}
}
边栏推荐
- Yolo target detection
- 基金销售行为规范及信息管理
- 未来十年世界数字化与机器智能展望
- What does the software test report contain? How to obtain high quality software test reports?
- 电商秒杀系统
- 微信小程序通过点击事件传参(data-)
- DNS server setup, forwarding, master-slave configuration
- [无线通信基础-13]:图解移动通信技术与应用发展-1-概述
- Ctfshow framework reproduction
- QQmlApplicationEngine failed to load component qrc:/main. qml:-1 No such file or directory
猜你喜欢
Jmeter跨线程参数关联无需脚本
远程办公期间,项目小组微信群打卡 | 社区征文
206 page Shanghai BIM Technology Application and development report 2021
What does the &?
KubeVela 1.4:让应用交付更安全、上手更简单、过程更透明
未来十年世界数字化与机器智能展望
Doker's container data volume
QQmlApplicationEngine failed to load component qrc:/main. qml:-1 No such file or directory
零样本和少样本学习
一次革命、两股力量、三大环节:《工业能效提升行动计划》背后的“减碳”路线图
随机推荐
Ideal interface automation project
基金客户和销售机构
Online customer service system code_ H5 customer service_ Docking with official account_ Support app_ Support for multiple languages
How to open a stock account? Is it safe to open a mobile account
Redis' transaction and locking mechanism
基金銷售行為規範及信息管理
后疫情时代,云计算如何为在线教育保驾护航
Achieve secure data sharing among multiple parties and solve the problem of asymmetric information in Inclusive Finance
Cesiumjs 2022 ^ source code interpretation [6] - new architecture of modelempirical
Code de conduite pour la vente de fonds et la gestion de l'information
理想中的接口自动化项目
Esp8266 becomes client and server
Prospects of world digitalization and machine intelligence in the next decade
C language array interception, C string by array interception method (c/s)
Schéma de transition asynchrone - générateur
D compile time count
35家巨头科技公司联合组成元宇宙标准论坛组织
MIT博士论文 | 优化理论与机器学习实践
d编译时计数
[Android, kotlin, tflite] mobile device integration depth learning light model tflite (image classification)