当前位置:网站首页>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];
}
}
边栏推荐
- Fastjson V2 simple user manual
- 未来十年世界数字化与机器智能展望
- 在线客服聊天系统源码_美观强大golang内核开发_二进制运行傻瓜式安装_附搭建教程...
- 【Android,Kotlin,TFLite】移动设备集成深度学习轻模型TFlite(物体检测篇)
- Redis的事务和锁机制
- 零样本和少样本学习
- Asynchronous transition scenario - generator
- [golang] golang implements the string interception function substr
- Redis' transaction and locking mechanism
- 多线程经典案例
猜你喜欢

Redis - 01 cache: how to use read cache to improve system performance?

5g smart building solution 2021

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

项目管理到底管的是什么?

Solution to the conflict between unique index and logical deletion

Prospects of world digitalization and machine intelligence in the next decade

Redis' transaction and locking mechanism

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

Achieve secure data sharing among multiple parties and solve the problem of asymmetric information in Inclusive Finance

The sandbox is being deployed on the polygon network
随机推荐
Redis' cache penetration, cache breakdown and cache avalanche
Online customer service system code_ H5 customer service_ Docking with official account_ Support app_ Support for multiple languages
flutter - sort List排序
Introduction to digital transformation solutions for enterprises going to sea
"More Ford, more China" saw through the clouds, and the orders of Changan Ford's flagship products exceeded 10000
How to mention hot fix and cherry pick
35 giant technology companies jointly form the meta universe standard Forum Organization
未来十年世界数字化与机器智能展望
Prospects of world digitalization and machine intelligence in the next decade
Meet the StreamNative | 杨子棵:是什么让我放弃了大厂 Offer
Lombok
Spark - understand partitioner in one article
基于kubernetes平台微服务的部署
智慧路灯| 云计算点亮智慧城市的“星星之火”
在线客服聊天系统源码_美观强大golang内核开发_二进制运行傻瓜式安装_附搭建教程...
[Android, kotlin, tflite] mobile device integration depth learning light model tflite (image classification)
In 2022, the latest JCR officially released the list of the latest global impact factors (top 600)
微信小程序通过点击事件传参(data-)
在线客服系统代码_h5客服_对接公众号_支持APP_支持多语言
5G智慧建筑解决方案2021