当前位置:网站首页>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];
}
}
边栏推荐
- 基金客户服务
- HP notebook disable touchpad after mouse is inserted
- Yolo target detection
- 有孚网络混合云,加速企业数字化转型升级
- MaxPool2d详解--在数组和图像中的应用
- Flitter - sort list sort
- Fund sales code of conduct and information management
- Qlineedit of QT notes (74) specifies the input type
- Discuz forum speed up to delete XXX under data/log PHP file
- CTFSHOW框架复现篇
猜你喜欢

Doker的容器数据卷

2022-06-30: what does the following golang code output? A:0; B:2; C: Running error. package main import “fmt“ func main() { ints := make

MIT doctoral dissertation optimization theory and machine learning practice

During telecommuting, the project team punched in the wechat group | solicited papers from the community

206 page Shanghai BIM Technology Application and development report 2021

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

"More Ford, more China" saw through the clouds, and the orders of Changan Ford's flagship products exceeded 10000

What does project management really manage?

In 2022, the latest JCR officially released the list of the latest global impact factors (top 600)
![Cesiumjs 2022 ^ source code interpretation [6] - new architecture of modelempirical](/img/ce/519778cd731f814ad111d1e37abd10.png)
Cesiumjs 2022 ^ source code interpretation [6] - new architecture of modelempirical
随机推荐
AtCoder Beginner Contest 255
零样本和少样本学习
206 page Shanghai BIM Technology Application and development report 2021
[Android, kotlin, tflite] mobile device integration deep learning light model tflite (object detection)
How cloud computing can protect online education in the post epidemic Era
leetcode:104. Maximum depth of binary tree
基金銷售行為規範及信息管理
理想中的接口自动化项目
Yolo target detection
电商秒杀系统
Esp8266 becomes client and server
Cesiumjs 2022 ^ source code interpretation [6] - new architecture of modelempirical
Fastjson V2 simple user manual
E-commerce seckill system
有孚网络混合云,加速企业数字化转型升级
“飞桨+辨影相机”成为AI界的“预制菜”,工业AI质检落地更简单
【Android,Kotlin,TFLite】移动设备集成深度学习轻模型TFlite(图像分类篇)
Two way data binding in wechat applet
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