当前位置:网站首页>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];
}
}
边栏推荐
- 在线客服聊天系统源码_美观强大golang内核开发_二进制运行傻瓜式安装_附搭建教程...
- 唯一性索引与逻辑删除冲突问题解决思路
- How to use dataant to monitor Apache APIs IX
- 微信小程序通过点击事件传参(data-)
- What is flush software? In addition, is it safe to open an account online now?
- 206页上海BIM技术应用与发展报告2021
- Redis - 01 缓存:如何利用读缓存提高系统性能?
- conv2d详解--在数组和图像中的使用
- 基金客户和销售机构
- "More Ford, more China" saw through the clouds, and the orders of Changan Ford's flagship products exceeded 10000
猜你喜欢

Redis - 01 缓存:如何利用读缓存提高系统性能?

In depth analysis of Apache bookkeeper series: Part 4 - back pressure

QQmlApplicationEngine failed to load component qrc:/main. qml:-1 No such file or directory

Jmeter跨线程参数关联无需脚本

Online customer service chat system source code_ Beautiful and powerful golang kernel development_ Binary operation fool installation_ Attached construction tutorial

5G智慧建筑解决方案2021

一次革命、两股力量、三大环节:《工业能效提升行动计划》背后的“减碳”路线图

Kubevela 1.4: make application delivery safer, easier to use, and more transparent

未来十年世界数字化与机器智能展望

How to use dataant to monitor Apache APIs IX
随机推荐
Jmeter跨线程参数关联无需脚本
How to design test cases
Kubevela 1.4: make application delivery safer, easier to use, and more transparent
股票开户要如何办理呢?办理手机开户安全吗
AtCoder Beginner Contest 257
Redis' transaction and locking mechanism
在线客服系统代码_h5客服_对接公众号_支持APP_支持多语言
flutter - sort List排序
Reason why wechat payment wxpaypubhelper V3 callback XML is empty
Ride: get picture Base64
The sandbox is being deployed on the polygon network
Zero sample and small sample learning
微信支付WxPayPubHelper v3版 回调xml为空的原因
10 airbags are equipped as standard, and Chery arizer 8 has no dead corner for safety protection
Architecture of IM integrated messaging system sharing 100000 TPS
Neo4j load CSV configuration and use
Meet the StreamNative | 杨子棵:是什么让我放弃了大厂 Offer
composer
"Paddle + camera" has become a "prefabricated dish" in the AI world, and it is easier to implement industrial AI quality inspection
How to open a stock account? Is it safe to open a mobile account