当前位置:网站首页>01 backpack DP
01 backpack DP
2022-06-26 15:54:00 【Honestbutter-】
A. Nine hours, nine people, nine doors ( 01 Knapsack for the number of solutions )
#include<iostream>
using namespace std;
const int N=1e5+100,mod=998244353;
int a[N],f[N][10];
int count(int x)
{
x%=9;
if(x==0) return 9;
return x;
}
int main()
{
int n;
cin>>n;
for(int i=1;i<=n;i++) cin>>a[i];
f[0][0]=1;
for(int i=1;i<=n;i++)
for(int j=0;j<=9;j++)
{
f[i][j]=(f[i][j]+f[i-1][j])%mod;
f[i][count(a[i]+j)]=(f[i][count(a[i]+j)]+f[i-1][j])%mod;
}
for(int i=1;i<=9;i++)
cout<<f[n][i]%mod<<" ";
return 0;
}
B-01 Knapsack for the number of solutions
Writing a : A two-dimensional
And the first scene A Do the same thing .
Be careful :
- d p [ i ] [ j ] dp[i][j] dp[i][j] Before presentation i i i A watermelon , The weight of the watermelon that can be pieced together is j j j The maximum number of schemes , Note that the two-dimensional array is opened a little larger according to the subject data range !!( At the beginning, the samples were all passed, but the answers were stuck here )
- d p [ 0 ] [ 0 ] = 1 dp[0][0]=1 dp[0][0]=1
- In three cases, three recurrence formulas are obtained
(1) The first i Choose no watermelon , d p [ i ] [ j ] + = d p [ i − 1 ] [ j ] dp[i][j]+=dp[i-1][j] dp[i][j]+=dp[i−1][j]
(2) Select No i i i A watermelon , What matters is j + w [ i ] j+w[i] j+w[i] and j + w [ i ] / 2 j+w[i]/2 j+w[i]/2
The first one is : d p [ i ] [ j + w [ i ] ] + = d p [ i − 1 ] [ j ] dp[i][j+w[i]] +=dp[i-1][j] dp[i][j+w[i]]+=dp[i−1][j]
The second kind : d p [ i ] [ j + w [ i ] / 2 ] + = d p [ i − 1 ] [ j ] dp[i][j+w[i]/2] +=dp[i-1][j] dp[i][j+w[i]/2]+=dp[i−1][j]
#include<iostream>
using namespace std;
const int N=1100,mod=1e9+7;
long long dp[N][5000],w[N];
int main()
{
int n,m;
cin>>n>>m;
for(int i=1;i<=n;i++) cin>>w[i];
dp[0][0]=1;
for(int i=1;i<=n;i++)
for(int j=0;j<=m;j++)
{
dp[i][j]=(dp[i][j]+dp[i-1][j])%mod;
dp[i][j+w[i]]=(dp[i][j+w[i]]+dp[i-1][j])%mod;
dp[i][j+w[i]/2]=(dp[i][j+w[i]/2]+dp[i-1][j])%mod;
}
for(int i=1;i<=m;i++) cout<<dp[n][i]%mod<<" ";
return 0;
}
Write two : One dimensional scrolling array
d p [ i ] [ j ] = ( d p [ i − 1 ] [ j ] + d p [ i − 1 ] [ j − w [ i ] ] + d p [ i − 1 ] [ j − w [ i ] / 2 ] ) dp[i][j]=(dp[i-1][j]+dp[i-1][j-w[i]]+dp[i-1][j-w[i]/2]) dp[i][j]=(dp[i−1][j]+dp[i−1][j−w[i]]+dp[i−1][j−w[i]/2])% m o d mod mod
Can be converted to a scrolling array , f o r for for Cycle the second layer j j j Write in reverse order ,
d p [ j ] = ( d p [ j ] + d p [ j − w [ i ] ] + d p [ j − w [ i ] / 2 ] ) dp[j]=(dp[j]+dp[j-w[i]]+dp[j-w[i]/2]) dp[j]=(dp[j]+dp[j−w[i]]+dp[j−w[i]/2])% m o d mod mod,
Add i f if if interpretation :
i f ( j ≥ w [ i ] ) , d p [ j ] = ( d p [ j ] + d p [ j − w [ i ] ] + d p [ j − w [ i ] / 2 ] ) if(j≥w[i]),dp[j]=(dp[j]+dp[j-w[i]]+dp[j-w[i]/2]) if(j≥w[i]),dp[j]=(dp[j]+dp[j−w[i]]+dp[j−w[i]/2])% m o d mod mod
i f ( j ≥ w [ i ] / 2 ) , d p [ j ] = ( d p [ j ] + d p [ j − w [ i ] ] / 2 ) if(j≥w[i]/2),dp[j]=(dp[j]+dp[j-w[i]]/2) if(j≥w[i]/2),dp[j]=(dp[j]+dp[j−w[i]]/2)% m o d mod mod
#include<iostream>
using namespace std;
const int N=1100,mod=1e9+7;
long long dp[N][5000],w[N];
int main()
{
int n,m;
cin>>n>>m;
for(int i=1;i<=n;i++) cin>>w[i];
dp[0][0]=1;
for(int i=1;i<=n;i++)
for(int j=0;j<=m;j++)
{
dp[i][j]=(dp[i][j]+dp[i-1][j])%mod;
dp[i][j+w[i]]=(dp[i][j+w[i]]+dp[i-1][j])%mod;
dp[i][j+w[i]/2]=(dp[i][j+w[i]/2]+dp[i-1][j])%mod;
}
for(int i=1;i<=m;i++) cout<<dp[n][i]%mod<<" ";
return 0;
}
边栏推荐
- 2022 Beijing Shijingshan District specializes in the application process for special new small and medium-sized enterprises, with a subsidy of 100000-200000 yuan
- Beijing Fangshan District specialized special new small giant enterprise recognition conditions, with a subsidy of 500000 yuan
- STEPN 新手入门及进阶
- El dialog drag and drop, the boundary problem is completely corrected, and the bug of the online version is fixed
- 学习内存屏障
- 11 cnn简介
- I want to know how to open an account through online stock? Is online account opening safe?
- Ansible自动化的运用
- AUTO sharding policy will apply DATA sharding policy as it failed to apply FILE sharding policy
- js文本滚动分散动画js特效
猜你喜欢
[CEPH] MKDIR | mksnap process source code analysis | lock state switching example
Panoramic analysis of upstream, middle and downstream industrial chain of "dry goods" NFT
Development, deployment and online process of NFT project (2)
PCIe Capabilities List
查词翻译类应用使用数据接口api总结
简单科普Ethereum的Transaction Input Data
Binding method of multiple sub control signal slots under QT
el-dialog拖拽,边界问题完全修正,网上版本的bug修复
svg环绕地球动画js特效
How to handle 2gcsv files that cannot be opened? Use byzer
随机推荐
Svg capital letter a animation JS effect
Summer camp is coming!!! Chongchongchong
A blog to thoroughly master the theory and practice of particle filter (PF) (matlab version)
Audio and video learning (III) -- SIP protocol
AUTO sharding policy will apply DATA sharding policy as it failed to apply FILE sharding policy
Solana扩容机制分析(1):牺牲可用性换取高效率的极端尝试 | CatcherVC Research
评价——TOPSIS
JS handwritten bind, apply, call
NFT 平台安全指南(2)
零知识 QAP 问题的转化
有Cmake的工程交叉编译到链接时报错找不到.so动态库文件
Evaluate:huggingface detailed introduction to the evaluation index module
在哪个平台买股票开户安全?求指导
粒子滤波 PF——在机动目标跟踪中的应用(粒子滤波VS扩展卡尔曼滤波)
STEPN 新手入門及進階
Svg rising Color Bubble animation
SVG大写字母A动画js特效
Why are encoder and decoder structures often used in image segmentation tasks?
Seurat to h5ad summary
NFT交易原理分析(1)