当前位置:网站首页>1175. prime number arrangement / Sword finger offer II 104 Number of permutations
1175. prime number arrangement / Sword finger offer II 104 Number of permutations
2022-06-30 23:12:00 【Biqiliang】
1175. Permutation of prime numbers 【 Simple questions 】【 A daily topic 】
Ideas :
Find the number of prime numbers k1,n-k1 Find the number of non prime numbers k2, Then the number of types meeting the requirements is k1 The factorial * k2 The factorial , Because the result of the operation may be out of bounds , Therefore, according to the meaning of the question, the result is 10^9+7 model .
set up MOD = 10^9+7, Therefore, the result of the above operation can be expressed as :
ans = (k1! * k2!) % MOD
Because the factorial operation may be out of bounds , Therefore, the above formula is mathematically transformed
ans = (k1! * k2!) % MOD
= ((k1! % MOD) * (K2! % MOD)) % MOD
//【】 Indicates cumulative multiplication
= ((【k1i % MOD】 % MOD ) * (【k2i % MOD】 % MOD ) ) % MOD
= (【k1i % MOD】 * 【k2i % MOD】) % MOD
Code :
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;
}
}
The finger of the sword Offer II 104. Number of permutations 【 Medium question 】
Ideas :【 Dynamic programming 】
I was cheated by the complete knapsack formula I recited yesterday ~, This is not a knapsack problem
Code :
class Solution {
public int combinationSum4(int[] nums, int target) {
// Definition dp Array ,dp[i] Express and for i The combination number of
int[] dp = new int[target+1];
// The boundary conditions i = 0 when , And for 0 There is only one combination of Select no element , therefore dp[0] = 1
dp[0] = 1;
// Transfer equation
for (int i = 1; i <= target; i++) {
for (int num : nums) {
// Traverse nums Array , If the target and i Is greater than or equal to the current element value num, Explain that this element can be selected dp[i] The value of is equal to all that satisfy the in the array i>=num Of dp Sum of values
if (i >= num) {
dp[i] += dp[i - num];
}
}
}
return dp[target];
}
}
边栏推荐
- 206 page Shanghai BIM Technology Application and development report 2021
- 【Android,Kotlin,TFLite】移动设备集成深度学习轻模型TFlite(物体检测篇)
- Prospects of world digitalization and machine intelligence in the next decade
- 异步過渡方案—Generator
- 如何使用 DataAnt 监控 Apache APISIX
- 唯一性索引与逻辑删除冲突问题解决思路
- Sm2246en+ SanDisk 15131
- Redis - 01 缓存:如何利用读缓存提高系统性能?
- AtCoder Beginner Contest 257
- Introduction to machine learning compilation course learning notes lesson 2 tensor program abstraction
猜你喜欢

MIT博士论文 | 优化理论与机器学习实践

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

HP notebook disable touchpad after mouse is inserted

分享十万级TPS的IM即时通讯综合消息系统的架构

Prospects of world digitalization and machine intelligence in the next decade

Spark - understand partitioner in one article

多線程經典案例

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

The sandbox is being deployed on the polygon network

Swift5.0 ----Swift FrameWork的创建及使用
随机推荐
How do I open a stock account on my mobile phone? In addition, is it safe to open a mobile account?
QQmlApplicationEngine failed to load component qrc:/main. qml:-1 No such file or directory
In depth analysis of Apache bookkeeper series: Part 4 - back pressure
shell 同时执行多任务下载视频
MIT doctoral dissertation optimization theory and machine learning practice
如何区分平台安全和网上炒作?网络投机有哪些止损技巧?
The Sandbox 正在 Polygon 网络上进行部署
Redis - 01 缓存:如何利用读缓存提高系统性能?
Solution to the conflict between unique index and logical deletion
股票开户要如何办理呢?办理手机开户安全吗
[golang] golang implements the string interception function substr
How to use dataant to monitor Apache APIs IX
Redis的缓存穿透、缓存击穿和缓存雪崩
手机上怎么开股票账户?另外,手机开户安全么?
Wechat applet transmits parameters (data-) by clicking events
How to design test cases
D compile time count
Shell multitasking to download video at the same time
E-commerce seckill system
PS2 handle-1 "recommended collection"