您的位置:首页 > 其它

ZCMU-1313-砝码称重

2017-01-12 12:02 288 查看

1313: The Balance

Time Limit: 1 Sec  Memory Limit: 128 MB
Submit: 32  Solved: 14

[Submit][Status][Web
Board]

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

HINT

For the second case,1=1,2=2,3=1+2,6=9-2-1,7=9-2,8=9-1,9=9,10=9+1,11=9+2,12=9+1+2,but,4,5 can not reach.

【解析】

这道题的意思就是给你n个砝码和n个砝码的重量。问你在这些砝码的总和当中那个质量是称不出来的。在这里我们又用到了母函数...模板是一样的就是拿来把它套出来。注意的是这里可以用砝码相减了。所以我们在代码中也要体现这一点,之前有个给你砝码的数量和质量让你求能称出什么来的。#include<cstdio>
#include<cstring>
#include<algorithm&g
cf39
t;
#include<cmath>
#include<map>
#include<string>
using namespace std;
int a[101],b[101];
int f1[11000],f2[11000];
int main()
{
int n,i,sum,m,j,k;
while(~scanf("%d",&n))
{
int cnt=0,sum1=0;
memset(f1,0,sizeof(f1));
memset(f2,0,sizeof(f2));
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
sum1+=a[i];//所以砝码质量的总和
}
f1[0]=f1[a[0]]=1;//此处f1[0]一定要注意为1,因为它本身的砝码肯定是能称出来的
sum=a[0];//当前砝码的质量总和
for(i=1;i<n;i++)
{
for(j=0;j<=sum;j++)//从0开始到当前加上的砝码的质量
{
for(k=0;k+j<=sum1&&k<=a[i];k+=a[i])//k+j最大肯定不可能比所有砝码质量加起来那么大。
{
m=abs(j-k);//由于砝码的质量也可以通过相减称出来,比如6可以是9克的砝码减去2克的和1克的
f2[j+k]+=f1[j];//模拟多项式相乘,下标表示指数相乘,
f2[m]+=f1[j];
}
}
sum=sum+a[i];
for(j=0;j<=sum;j++)
{
f1[j]=f2[j];//多项式的结果给了f1数组
f2[j]=0;
}
}
int p=0,flag=0;
for(i=1;i<=sum;i++)
{
if(f1[i]==0)
{
cnt++;
b[p++]=i;
flag=1;
}
}
printf("%d\n",cnt);
if(flag)
{
printf("%d",b[0]);
for(i=1;i<p;i++)
printf(" %d",b[i]);
printf("\n");
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: