HDoj-1709-The Balance-母函数

The Balance

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 5750    Accepted Submission(s): 2337

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

3
1 2 4
3
9 2 1

Sample Output

0
2
4 5
<span style="font-family: 'Microsoft YaHei', 微软雅黑, Lucida, Verdana, 'Hiragino Sans GB', STHeiti, 'WenQuanYi Micro Hei', SimSun, sans-serif; font-size: 14px; line-height: 24px;">这里比较特殊的一点是砝码可以放在天枰的左右两端,我们可以在c2[j+k]+=c1[j]</span><br style="font-family: 'Microsoft YaHei', 微软雅黑, Lucida, Verdana, 'Hiragino Sans GB', STHeiti, 'WenQuanYi Micro Hei', SimSun, sans-serif; font-size: 14px; line-height: 24px;" /><span style="font-family: 'Microsoft YaHei', 微软雅黑, Lucida, Verdana, 'Hiragino Sans GB', STHeiti, 'WenQuanYi Micro Hei', SimSun, sans-serif; font-size: 14px; line-height: 24px;">后加多一句c2[abs(j-k)]+=c[j]...即可,假设原来的砝码都放在右端,则可以把新加的砝码放在左端,得到新重量。</span>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<cmath>
int main()
{
	int c1[15000],c2[15000],a[150],count[15000];
	int n,i,j,k,s,counts;
	while(~scanf("%d",&n))
	{
		int s=0;
		for(i=0;i<n;i++)
		{
			scanf("%d",&a[i]);
			s+=a[i];
		}
		memset(c1,0,sizeof(c1));
		memset(c2,0,sizeof(c2));

		c1[0]=c1[a[0]]=1;
		int end=a[0];
		for(i=2;i<=n;i++)
		{

			for(j=0;j<=end;j++)
			{
				for(k=0;j+k<=s && k<=a[i-1];k+=a[i-1])
				{
					c2[j+k]+=c1[j];
					c2[abs(j-k)]+=c1[j];//系数可能为负数
				}
			}
			end+=a[i-1];
			memcpy(c1,c2,sizeof(c1));
			memset(c2,0,sizeof(c2));

		}
		int cnt=0;
		for(i=0;i<=s;i++)
		{
			if(c1[i]==0)
			{

				c2[cnt++]=i;
			}

		}
		if(cnt==0)
            printf("0\n");
		else
		{
	    	printf("%d\n",cnt);
			for(i=0;i<cnt-1;i++)
			{
				printf("%d ",c2[i]);
			}
			printf("%d\n",c2[i]);
		}
	}
	return 0;
} 
时间: 2024-11-05 06:31:48

HDoj-1709-The Balance-母函数的相关文章

Hdu 1709 The Balance

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1709 题意: 给N个整数,每个数只能使用一次.将他们组合起来,最后看在1~sum(a[1]..a[N])这些数里有多少数是这N个数组合不出来的. 先输出这些数的个数,再将这些数输出来.如果个数是0,那么不需要输出数. 案例分析: input: 3 1 2 4 output: 0 -->1(||4-1-2) , 2(||4-2) , 1+2(||4-1) , 4  , 1+4 , 2+4 , 1+2+

组合数学 - 母函数的运用 --- hdu 1709 :The Balance

The Balance Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 5706    Accepted Submission(s): 2311 Problem Description Now you are asked to measure a dose of medicine with a balance and a number o

hdu 1709 The Balance(母函数)

题意: 有一个天平.有N个砝码.重量分别是A1...AN. 问重量[1..S]中有多少种重量是无法利用这个天平和这些砝码称出来的. S是N个砝码的重量总和. 思路: 对于每一个砝码来说,有三种:不放,放左盘,放右盘. 额,,母函数和DP其实核心一样,,,, 看代码,, 代码: int n; int aa[105]; bool a[10005], b[10005]; int temp[10005]; int main(){ while(scanf("%d",&n)!=EOF){

杭电 HDU ACM 1709 The Balance

The Balance Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 6304    Accepted Submission(s): 2592 Problem Description Now you are asked to measure a dose of medicine with a balance and a number

HDU 1709 The Balance( DP )

The Balance Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 5956    Accepted Submission(s): 2427 Problem Description Now you are asked to measure a dose of medicine with a balance and a number o

HDOJ Fruit 2152【母函数】

Fruit Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3899    Accepted Submission(s): 2217 Problem Description 转眼到了收获的季节,由于有TT的专业指导,Lele获得了大丰收.特别是水果,Lele一共种了N种水果,有苹果,梨子,香蕉,西瓜--不但味道好吃,样子更是好看. 于是

母函数专题

1.Hdu 2082 找单词 题意:单词A-Z具有1-26的价值,现有字母A-Z的个数num[i],求问在不超过价值为五十的情况下,有多少种字母的组合数. 思路:用指数代表价值,价值又为数组的下标:用系数代表组成该价值的方案数,方案数为数组中存的值. 1 #include<iostream> 2 #include<memory.h> 3 using namespace std; 4 int re[55]; 5 int p1[55]; 6 int p2[55]; 7 int xx[2

HDU 1709 母函数天平问题 可出现减法的情况 The Balance

The Balance Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 6652    Accepted Submission(s): 2730 Problem Description Now you are asked to measure a dose of medicine with a balance and a number o

HDU1709 The Balance 【母函数】

The Balance Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 5463    Accepted Submission(s): 2214 Problem Description Now you are asked to measure a dose of medicine with a balance and a number

HDOJ 2189 悼念512汶川大地震遇难同胞——来生一起走 【母函数】

题意:很清楚不解释. 策略:如题, 就是个简单的母函数的改变. 这道题做了好久,才明白是那有毛病,还是理解的不够深刻. AC代码: #include<stdio.h> #include<string.h> int c1[155], c2[155]; int pri[150] = {3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103,