acwing 278 number combination

Topic

Insert picture description here

Problem solution (seeking the number of solutions)

Insert picture description here

State transition equation: f [i, j] = f [i-1] [j] + f [i] [j-v] (Find the number of solutions)
Initialization:

f [i] [0] = 1 (The number of solutions selected from the first i items with a volume of 0 is 1)
f [0] [i] = 0 (The number of solutions selected from the first 0 items with a volume of i is 0)

Code

#include<bits/stdc++.h>

using namespace std;
const int N = 1e4 + 10;

int f[N];

int main() {
    
    

    int n, m;
    cin >> n >> m;
    f[0] = 1;
    for (int i = 1; i <= n; i++) {
    
    
        int v;
        cin >> v;
        for (int j = m; j >= v; j--) {
    
    
            f[j] += f[j - v];
        }
    }

    cout << f[m] << endl;

    return 0;
}

Guess you like

Origin blog.csdn.net/qq_44791484/article/details/115261881