AcWing 11. 背包问题求方案数

#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
#define gdb(x) cout<<x<<endl;
const int mod = 1e9 + 7;
const int N = 1010;
int f[N],g[N];
int main() {
    int n,m;
    cin >> n >> m;
    fill(f, f + N, -0x3f);
    f[0] = 0;
    g[0] = 1;
    int v, w;
    for(int i = 0; i < n; ++i) {
        cin >> v >> w;
        for(int j = m; j >= v; --j) {
            int val = max(f[j], f[j - v] + w);
            int cnt = 0;
            if(f[j] == val) cnt = (cnt + g[j]) % mod;
            if(f[j - v] + w == val) cnt = (cnt + g[j - v]) % mod;
            f[j] = val;
            g[j] = cnt;
        }
    }
    int val = 0;
    for(int i = 0; i <= m; ++i) val = max(val, f[i]);
    int cnt = 0;
    for(int i = 0; i <= m; ++i) if(val == f[i]) cnt = (cnt + g[i]) % mod;
    cout << cnt;
    return 0;
}
上一篇:vue3 reactive函数用法


下一篇:dp状态压缩