当前位置:网站首页>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;
}
边栏推荐
- When a project with cmake is cross compiled to a link, an error cannot be found So dynamic library file
- How to identify contractual issues
- 2022 Beijing Shijingshan District specializes in the application process for special new small and medium-sized enterprises, with a subsidy of 100000-200000 yuan
- Evaluate:huggingface评价指标模块入门详细介绍
- CNN优化trick
- Svg animation around the earth JS special effects
- 为什么图像分割任务中经常用到编码器和解码器结构?
- Is it safe to open an account for mobile stock registration? Is there any risk?
- NFT 平台安全指南(2)
- JVM笔记
猜你喜欢

Particle filter PF - 3D CV target tracking with uniform motion (particle filter vs extended Kalman filter)

IntelliJ idea -- Method for formatting SQL files

音视频学习(三)——sip协议

如何配置使用新的单线激光雷达

How to configure and use the new single line lidar

11 cnn简介

2Gcsv文件打不开怎么处理,使用byzer工具

Everyone is a scientist free gas experience Mint love crash

STEPN 新手入门及进阶

NFT contract basic knowledge explanation
随机推荐
【leetcode】701. Insert operation in binary search tree
IDEA本地代理后,无法下载插件
【leetcode】112. Path sum - 113 Path sum II
4 自定义模型训练
TweenMax+SVG切换颜色动画场景
有Cmake的工程交叉编译到链接时报错找不到.so动态库文件
「幹貨」NFT 上中下遊產業鏈全景分析
Unable to download Plug-in after idea local agent
AUTO sharding policy will apply DATA sharding policy as it failed to apply FILE sharding policy
2022 Beijing Shijingshan District specializes in the application process for special new small and medium-sized enterprises, with a subsidy of 100000-200000 yuan
Interview pit summary I
Canvas three dot flashing animation
IntelliJ idea -- Method for formatting SQL files
[wechat applet] event binding, do you understand?
Audio and video learning (III) -- SIP protocol
Is it safe to open an account for mobile stock registration? Is there any risk?
js文本滚动分散动画js特效
JS handwritten bind, apply, call
On which platform is it safe to buy shares and open an account? Ask for guidance
Reflection modification final