hdu 1709 The Balance

来源:互联网 发布:金山在线数据恢复收费 编辑:程序博客网 时间:2024/06/06 08:24

The Balance

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


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
31 2 439 2 1
 

Sample Output
024 5
 

这道题与常规的凑数字不同,各个数字之间不仅仅是加起来了,相减也是可以的,范围是1到所有数的总和,在这个范围里面求出所有凑不到的数字,只要在原有的基础上,加上一个相减的情况就可以了,就是加上now[abs(k-j)]+=ans[j];



#include<iostream>#include<cstring>#include<cmath>#include<cstdio>#include<algorithm>using namespace std;int main(){    int n;    while(scanf("%d",&n)!=EOF)    {        int a[11111];        int a1[111111];        int a2[111111];        memset(a1,0,sizeof(a1));        memset(a2,0,sizeof(a2));        int sum=0;        int i,j,k;        for(i=1;i<=n;i++)        {            cin>>a[i];            sum+=a[i];        }        a1[a[1]]=1;        a1[0]=1;        for(i=2;i<=n;i++)        {            for(j=0;j<=sum;j++)            {                for(k=0;k<=a[i];k+=a[i])                {                    a2[k+j]+=a1[j];                    a2[abs(k-j)]+=a1[j];                }                            }            for(j=0;j<=sum;j++)            {                a1[j]=a2[j];                a2[j]=0;            }        }        int ans[1111];        int l=0;        int ans1=0;        for(i=0;i<=sum;i++)        {            if(a1[i]==0)            {                ans1++;                ans[l++]=i;            }        }        if(ans1==0)cout<<"0"<<endl;        else {    int q=0;        cout<<ans1<<endl;        for(i=0;i<l;i++)        {               if(q++)cout<<" ";            cout<<ans[i];            }                cout<<endl;        }    }    return 0;} 



原创粉丝点击