首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >【HDU】1709 - The Balance(母函数)

【HDU】1709 - The Balance(母函数)

作者头像
FishWang
发布2025-08-27 08:57:33
发布2025-08-27 08:57:33
15900
代码可运行
举报
运行总次数:0
代码可运行

点击打开题目

The Balance

Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 7474 Accepted Submission(s): 3101

Problem Description

Now you are asked to measure a dose of medicine with a balance and a number of weights. Certainly it is not always achievable. So you should find out the qualities which cannot be measured from the range [1,S]. S is the total quality of all the weights.

Input

The input consists of multiple test cases, and each case begins with a single positive integer N (1<=N<=100) on a line by itself indicating the number of weights you have. Followed by N integers Ai (1<=i<=N), indicating the quality of each weight where 1<=Ai<=100.

Output

For each input set, you should first print a line specifying the number of qualities which cannot be measured. Then print another line which consists all the irrealizable qualities if the number is not zero.

Sample Input

代码语言:javascript
代码运行次数:0
运行
复制
   3
1 2 4
3
9 2 1

Sample Output

代码语言:javascript
代码运行次数:0
运行
复制
   0
2
4 5

Source

HDU 2007-Spring Programming Contest

同样是天平用母函数的问题,思路同上一篇博客。

代码如下:

代码语言:javascript
代码运行次数:0
运行
复制
#include <cstdio>
#include <cstring>
#define CLR(a) memset(a,0,sizeof(a))
int abs(int x)
{
	return x < 0 ? -x : x;
}
int main()
{
	int n;
	int sum;
	int w[111];
	int c[10011];
	int t[10011];
	int ant;
	int num[10011];
	while (~scanf ("%d",&n))
	{
		sum = 0;
		ant = 0;
		CLR(c);
		CLR(t);
		for (int i = 1 ; i <= n ; i++)
		{
			scanf ("%d",&w[i]);
			sum += w[i];
		}
		c[0] = c[w[1]] = 1;
		for (int i = 2 ; i <= n ; i++)
		{
			for (int j = 0 ; j <= sum ; j++)
				for (int k = 0 ; k <= w[i] && k+j <= sum ; k += w[i])
				{
					t[j+k] += c[j];
					t[abs(j-k)] += c[j];
				}
			for (int j = 0 ; j <= sum ; j++)
			{
				c[j] = t[j];
				t[j] = 0;
			}
		}
		for (int i = 1 ; i <= sum ; i++)
		{
			if (c[i] == 0)
				num[ant++] = i;
		}
		if (ant)
		{
			printf ("%d\n",ant);
			for (int i = 0 ; i < ant-1 ; i++)
				printf ("%d ",num[i]);
			printf ("%d\n",num[ant-1]);
		}
		else
			printf ("0\n");
	}
	return 0;
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2025-08-26,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • The Balance
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档