码迷,mamicode.com
首页 > 其他好文 > 详细

HDoj-1709-The Balance-母函数

时间:2014-10-30 22:48:31      阅读:247      评论:0      收藏:0      [点我收藏+]

标签:acm   iostream   c++   c   cstring   

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;
} 


HDoj-1709-The Balance-母函数

标签:acm   iostream   c++   c   cstring   

原文地址:http://blog.csdn.net/holyang_1013197377/article/details/40625887

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!