ch_5201 数字组合

01背包

#include<iostream>
#include<cstdio>

#define ri register int
#define u int

namespace opt {

    inline u in() {
        u x(0),f(1);
        char s(getchar());
        while(s<'0'||s>'9') {
            if(s=='-') f=-1;
            s=getchar();
        }
        while(s>='0'&&s<='9') {
            x=(x<<1)+(x<<3)+s-'0';
            s=getchar();
        }
        return x*f;
    }

}

using opt::in;

#define NN 105
#define MM 10005

#include<algorithm>

namespace mainstay {

    u N,M,a[NN],f[MM]={1};

    inline void solve() {
        std::cin>>N>>M;
        for(ri i(1);i<=N;++i) std::cin>>a[i];
        for(ri i(1);i<=N;++i){
            for(ri j(M);j>=a[i];--j){
                f[j]+=f[j-a[i]];
            }
        }
        std::cout<<f[M];
    }

}

int main() {

    //freopen("x.txt","r",stdin);
    std::ios::sync_with_stdio(false);
    mainstay::solve();

}
原文地址:https://www.cnblogs.com/ling-zhi/p/11805755.html