
The Balance
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4737 Accepted Submission(s): 1895
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
2
4 5
母函数,有左右算法....很好的题目..
思路...先选着一个点作为‘0’点,然后对左右放置之后的数组(即幂)进行右移动.....最后统计.....很吊的一道母函数呀!!,题目非常新颖.....
#include<iostream>
#include<vector>
#include<cstring>
#define maxn 20003
#define gong 10000
using namespace std;
int c1[maxn],c2[maxn];
int save[maxn];
int main()
{
int n,i,sum,j,k,count;
while(cin>>n)
{
vector<int>arr(n);
memset(c1,,sizeof(c1));
memset(c2,,sizeof c2);
memset(save,,sizeof save);
count=sum=;
for(i=;i<n;i++)
{
cin>>arr[i];
sum+=arr[i];
}
for(i=-arr[];i<=arr[];i+=arr[])
{
c1[i+gong]=;
}
for(i=;i<n;i++)
{
for(j=-sum;j<=sum;j++)
{
for(k=-arr[i];k<=arr[i]&&k+j<=sum;k+=arr[i])
{
c2[k+j+gong]+=c1[j+gong];
}
} for(j=-sum+gong;j<=sum+gong;j++)
{
c1[j]=c2[j];
c2[j]=;
}
}
int ans=;
for(j=;j<=sum;j++)
{
if(c1[j+gong]==||c1[gong-j]==)
{
save[ans++]=j;
}
}
cout<<ans<<endl;
if(ans)
{
for(i=;i<ans;i++)
{
if(i==)
cout<<save[i];
else
cout<<" "<<save[i];
}
cout<<endl;
}
}
return ;
}