洛谷试炼场P1060 (动态规划,01背包)

题目连接https://www.luogu.org/problemnew/show/P1060

#include<bits/stdc++.h>
using namespace std;
int a[105];
int dp[10009];
int main()
{
	//freopen("t.txt","r",stdin);
	int n,m;
	scanf("%d%d",&n,&m);
	for(int i=0;i<n;i++)
	{
		scanf("%d",&a[i]);
	}
	memset(dp,0,sizeof(dp));
	dp[0]=1;
	int tot=0;
	for(int i=0;i<n;i++)
	{
		for(int j=tot;j>=0;j--)
		{
			if(j+a[i]<=m&&dp[j])
			{
				dp[j+a[i]]+=dp[j];
			}
		}
		tot+=a[i];
		if(tot>m)
		tot=m;
	}
	printf("%d",dp[m]);
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_39861441/article/details/89787530