当前位置:网站首页>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];
}
}
边栏推荐
- Architecture of IM integrated messaging system sharing 100000 TPS
- Two dots on the top of the latex letter
- How to open a stock account? Is it safe to open a mobile account
- 零样本和少样本学习
- 基金管理人公司治理和风险管理
- conv2d详解--在数组和图像中的使用
- 远程办公期间,项目小组微信群打卡 | 社区征文
- Strictly minor spanning tree
- 严格次小生成树
- Cesiumjs 2022 ^ source code interpretation [6] - new architecture of modelempirical
猜你喜欢

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

Redis的事务和锁机制

206 page Shanghai BIM Technology Application and development report 2021

Fastjson V2 简单使用手册

In 2022, the latest JCR officially released the list of the latest global impact factors (top 600)

Ideal interface automation project

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

What does the &?

【Android,Kotlin,TFLite】移动设备集成深度学习轻模型TFlite(物体检测篇)

分享十万级TPS的IM即时通讯综合消息系统的架构
随机推荐
云游戏| 云计算推动游戏行业进入“新纪元”
5G智慧建筑解决方案2021
"Paddle + camera" has become a "prefabricated dish" in the AI world, and it is easier to implement industrial AI quality inspection
机器学习编译入门课程学习笔记第二讲 张量程序抽象
多线程经典案例
电商秒杀系统
Two way data binding in wechat applet
HP notebook disable touchpad after mouse is inserted
Neo4j load CSV configuration and use
Pycharm is very fast to learn from installation to full armament. There are so many pictures because it is too detailed!
[golang] golang实现截取字符串函数SubStr
Asynchronous transition scenario - generator
Reason why wechat payment wxpaypubhelper V3 callback XML is empty
Fastjson V2 简单使用手册
企业出海数字化转型解决方案介绍
How to mention hot fix and cherry pick
Netease cloud sign in lottery? That year I could sign in for 365 days. No? Look.
微信小程序中的数据双向绑定
MaxPool2d详解--在数组和图像中的应用
10 airbags are equipped as standard, and Chery arizer 8 has no dead corner for safety protection