当前位置:网站首页>背包问题求方案数
背包问题求方案数
2022-06-26 17:05:00 【Zqchang】
目录
思想
与最短路一样,因为dp就是拓扑图跑最短路
最短路中,如果有两个点到终点的距离相同,那么这两个点方案数加在一起就是答案
回忆一下状态转移方程
这里的状态就是,不超过j时的最大价值
#include <cstring>
#include <iostream>
using namespace std;
const int N = 1010, mod = 1e9 + 7;
int n, m;
int f[N], g[N];
int main()
{
cin >> n >> m;
f[0] = 0;
g[0] = 1;
for (int i = 0; i < n; i ++ )
{
int v, w;
cin >> v >> w;
for (int j = m; j >= v; j -- )
{
int maxv = max(f[j], f[j - v] + w);
int s = 0;
if (f[j] == maxv) s = g[j];
if (f[j - v] + w == maxv) s = (s + g[j - v]) % mod;
f[j] = maxv, g[j] = s;
}
}
int sum = 0;
for (int i = 0; i <= m; i ++ )
if (f[i] == f[m])
sum = (sum + g[i]) % mod;
cout << sum << endl;
return 0;
}
边栏推荐
- Day10 daily 3 questions (3): String Matching in array
- Cache breakdown! Don't even know how to write code???
- Day10 daily 3 questions (1): sum gradually to get the minimum value of positive numbers
- Leetcode HOT100 (22--- bracket generation)
- Quantitative contract system development analysis case - detailed explanation of contract quantitative system development scheme
- Platform management background and merchant menu resource management: Design of platform management background data service
- 经典同步问题
- [matlab project practice] prediction of remaining service life of lithium ion battery based on convolutional neural network and bidirectional long short time (cnn-lstm) fusion
- The high concurrency system is easy to play, and Alibaba's new 100 million level concurrent design quick notes are really fragrant
- 量化合约系统开发分析案例丨合约量化系统开发方案详解
猜你喜欢

Cache breakdown! Don't even know how to write code???

20:第三章:开发通行证服务:3:在程序中,打通redis服务器;(仅仅是打通redis服务器,不涉及具体的业务开发)

Incomplete line spacing adjustment of formula display in word

Decentralized NFT transaction protocol will defeat opensea

Platform management background and merchant menu resource management: access control design of platform management background

Getting started with mongodb

Over the weekend: 20000 words! Summary of JVM core knowledge, 18 serial cannons as a gift

丰富专业化产品线, 江铃福特领睿·极境版上市

Introduction to distributed cache / cache cluster
![[recommendation system learning] technology stack of recommendation system](/img/ff/afc6f4b0997cfcb9e01ffbebf2a872.png)
[recommendation system learning] technology stack of recommendation system
随机推荐
Turtle cartography
Getting started with mongodb
SIGIR 2022 | University of Hong Kong and others proposed the application of hypergraph comparative learning in Recommendation System
玩转Linux,轻松安装配置MySQL
【uniapp】uniapp手机端使用uni.navigateBack失效问题解决
Implementation of MySQL master-slave architecture
Distributed Architecture Overview
分布式缓存/缓存集群简介
类型多样的石膏PBR多通道贴图素材,速来收藏!
Day10 daily 3 questions (1): sum gradually to get the minimum value of positive numbers
Decentralized NFT transaction protocol will defeat opensea
Fgetc() reads content from file
Microservice architecture practice: business management background and SSO design, SSO client design
Find out the maximum value of each column element of NxN matrix and store it in the one-dimensional array indicated by formal parameter B in order
Programmer interview guide - self introduction
What is the difference between digital collections and NFT
Introduction to minimal API
Viewing the task arrangement ability of monorepo tool from turborepo
What is the preferential account opening policy of securities companies now? Is it safe to open an account online now?
SQL injection for Web Security (3)