20分悬赏一个赞
  • 板块P1441 砝码称重
  • 楼主gzwjj
  • 当前回复0
  • 已保存回复0
  • 发布时间2024/8/1 10:52
  • 上次更新2024/8/1 11:34:09
查看原帖
20分悬赏一个赞
704414
gzwjj楼主2024/8/1 10:52
#include <bits/stdc++.h>
using namespace std;

const int N = 2005, INF = 0x3f3f3f3f;
int dp[N], a[N], f[N], cnt, tot, n, m; 
void backpack()
{
	memset(dp, 0, sizeof(dp));
	dp[0] = 1;
	cnt = tot = 0;
	for(int i = 0; i < n; i++)
	{
		if (f[i] != 0) continue;
		for (int j = tot; j >= 0; j--)
		{
			if (dp[j] == 1 && dp[j+a[i]] == 0)
			{
				dp[j+a[i]] = 1;
				cnt++;
			}
		}
		tot += a[i];
	} 
}
void dfs(int step, int last) 
{
	if (last > m) return;
	//cout << step << " " << last << endl; 
	if (step == n)
	{
		if (last == m)
		{
			//cout << last << endl;
			backpack();
		}
		return; 
	} 
	dfs(step+1, last);
	f[step] = 1; 
	dfs(step+1, last+1);
	f[step] = 0; 
}

int main()
{
	cin >> n >> m;
	for (int i = 0; i < n; i++) 
	{
		cin >> a[i];
	}
	dfs(1, 0);
	cout << cnt << endl;
	return 0;
} 
2024/8/1 10:52
加载中...