AcWing 278. 数字组合
原题链接
简单
大法师
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
int n,m,ans = 0;
int v[1002];
int dfs(int a , int b){
if(b==0){
return 1;
}
if(a>n || b<0)return 0;
return dfs(a+1 , b) + dfs(a+1 , b-v[a]);
}
int main()
{
cin>>n>>m;
for(int i = 1; i <= n; i ++ )cin>>v[i];
cout<<dfs(1,m)<<endl;
return 0;
}
y总的dp
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
int n,m,v;
int dp[10010];2
int main()
{
cin>>n>>m;
dp[0] = 1;
for(int i = 1; i <= n; i ++ ){
cin>>v;
for(int j = m; j >= v; j -- ){
dp[j]+=dp[j-v];
}
}
cout<<dp[m]<<endl;
return 0;
}