其他分享
首页 > 其他分享> > AcWing 11. 背包问题求方案数

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;
}

标签:11,cnt,背包,val,int,0x3f,mod,include,AcWing
来源: https://www.cnblogs.com/lemonbiscuit/p/15939909.html