您的位置:首页 > 其它

hdu 1709 The Balance

2012-08-10 11:00 274 查看
http://acm.hdu.edu.cn/showproblem.php?pid=1709

View Code

/*
砝码称重
*/

#include<stdio.h>
#include<string.h>
int f(int x,int y)
{
if(x>y) return x-y;
else return y-x;
}
int main()
{

int n;
int p[10010];
int sum[10010];
int temp[10010];
int a[105];
int i,j,k,l;
int totall;
while(~scanf("%d",&n))
{
int count=0;
l=0;
memset(sum,0,sizeof(sum));
memset(temp,0,sizeof(temp));
totall=0;
for(i=1;i<=n;i++)
{
scanf("%d",&a[i]);
totall+=a[i];
}
sum[a[1]]=1;
sum[0]=1;//注意不要漏这个初始化
for(i=2;i<=n;i++)
{
for(j=0;j<=totall;j++)
{
for(k=0;k<=a[i];k+=a[i])
{
temp[j+k]+=sum[j];
temp[f(j,k)]+=sum[j];//砝码不一定放在一边
}
}
for(j=0;j<=totall;j++)
{

sum[j]=temp[j];
temp[j]=0;

}
}

for(i=1;i<=totall;i++)
{
if(sum[i]==0)
{
count++;
p[l++]=i;
}
}
if(count)
{
printf("%d\n",count);
printf("%d",p[0]);
for(i=1;i<l;i++)
printf(" %d",p[i]);
printf("\n");
}
else printf("0\n");

}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: